1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How can I pass a variable from CSV file to Oracle SQL query fetch in Python?

Discussão em 'Python' iniciado por Stack, Setembro 10, 2024.

  1. Stack

    Stack Membro Participativo

    I have the following piece of code where I read a csv file and connect to the database. then I want to pass two columns from CSV file as variable to my query and eventually convert the result to a pd database.

    I have tried different ways of binding and converted columns to list but I was unsuccessful. with this piece of code I get the following Error:

    DatabaseError: DPY-4010: a bind variable replacement value
    for placeholder ":HOUR" was not provided


    or I get below error when I add this part to execute():

    res = connection.cursor().execute("SELECT HOUR,UNITSCHEDULEID,VERSIONID,MINRUNTIME FROM
    int_Stg.UnitScheduleOfferHourly WHERE Hour in :1 AND UnitScheduleId in :2", hour, unitid)


    TypeError: Cursor.execute() takes from 2 to 3 positional arguments but 4 were given


    the following is the code I execute:

    import pandas as pd
    import numpy as np

    df = pd.read_csv(r"csv.csv")

    df=df.dropna()

    unitid=df['UNITSCHEDULEID'].unique()
    hour=df['GMT_DATETIME'].unique()

    import os, oracledb, csv, pyodbc
    TNS_Admin = os.environ.get('TNS_Admin', r'\\corp.xxx\oracle')
    oracledb.defaults.config_dir = TNS_Admin
    pw = input(f'Enter password: ')
    connection = oracledb.connect(user='xxxxx', password= pw, dsn="World")

    res = connection.cursor().execute("SELECT HOUR,UNITSCHEDULEID,VERSIONID,MINRUNTIME FROM
    int_Stg.UnitScheduleOfferHourly WHERE Hour in :Hour AND UnitScheduleId in :unitid").fetchall()
    print(res)

    connection.close()

    Continue reading...

Compartilhe esta Página