0

I want to generate dynamic insert statements using python script. I will be using the data from a csv file to get the inserts: so far this is what I am able to accomplish via google:

import csv
openFile = open('test.csv', 'r')
csvFile = csv.reader(openFile)
header = next(csvFile)
headers = map((lambda x: '`'+x+'`'), header)
insert = 'INSERT INTO Table (' + ", ".join(headers) + ") VALUES "
for row in csvFile:
    values = map((lambda x: '"'+x+'"'), row)
    print (insert +"("+ ", ".join(values) +");" )
openFile.close()

the insert statements should have single quotes only on strings and not on numbers.

Ritesh
  • 85
  • 8
  • What RDBMS are you using? PostgreSQL and MySQL both have specialised syntax for loading CSV data. I'm sure others do as well. – Dunes Apr 15 '18 at 14:49
  • I am using oracle for now.I do not want to fire the insert the statements directly on the database. I want to just save the output in a .sql file – Ritesh Apr 15 '18 at 14:51
  • You might find this useful -- https://stackoverflow.com/questions/6198863/oracle-import-csv-file – Dunes Apr 15 '18 at 14:56
  • I was looking more of a program where I can generate statements like delete and inserts – Ritesh Apr 16 '18 at 11:24

0 Answers0