0

I have the following Python code extract results to csv from an oracle database.

I have a txt file that contains a list of ID's Example:

ID

ID124543

ID124544

ID124545

I want to read each ID from a file, insert it into a sql query as a variable.

SQL="SELECT ID, FIELD1, FIELD2 FROM SOME_TABLE WHERE ID=variable"

Help me, maybe someone solved this problem. Tell me how to correctly associate variables with a sql query? I am using cx_Oracle.

Sam Chats
  • 2,271
  • 1
  • 12
  • 34
Arthur Brown
  • 5
  • 1
  • 3
  • Possible duplicate of [Python cx\_Oracle bind variables](https://stackoverflow.com/questions/32868717/python-cx-oracle-bind-variables) – OldProgrammer Jun 25 '17 at 19:53

1 Answers1

0

Something like the following may work for you. IIRC it came from http://www.oracle.com/technetwork/articles/prez-python-dataparsing-087750.html

import csv
import cx_Oracle

db = cx_Oracle.connect("hr", "hrpwd", "localhost/XE")
cursor = db.cursor()
cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'")

reader = csv.reader(open("job_history.csv", "r"))
lines = []
for line in reader:
    lines.append(line)

cursor.executemany("INSERT INTO job_his VALUES(:1,:2,:3,:4,:5)", lines)
db.commit()
Christopher Jones
  • 9,449
  • 3
  • 24
  • 48