+ 1

How to write VALUES statement in a insert statement in Python?🙄

Creating a script where I am reading from csv file and iterating all the rows in the csv file but i need help to generate insert statements of each row and I am also output it that into a file. So my only issue is the generate insert statements of each row. Thanks for the help. ------------------------------------------------------------------------ here is my code: ------------------------------------------------------------------------ import csv openFile = open('data.csv', 'r') csvFile = csv.reader(openFile) header = next(csvFile) headers = map((lambda x: '"' + x + '"'), header) insert = 'INSERT INTO my_table (' + ", ".join(headers) + ") VALUES " values = [] for row in csvFile: #this line below values.append('(' + ','.join(map(lambda x: '"%s"' % x, row)) + ')') data = insert + ("".join(values)) print(data) createOnFile = open("data.txt","w+") createOnFile.write(data) createOnFile.close() openFile.close() -------------------------------------------------------------------- Thanks for the help.

20th Dec 2020, 2:23 AM
Senithu Sethmin👿
Senithu Sethmin👿 - avatar
1 Resposta
+ 4
What output you get from the print(data) statement? I need to know your SQL table structure to know whether the query is valid I also need to know which DBMS you are using, because different DBMS uses different approach for multiple row insertion.
20th Dec 2020, 3:16 AM
Ipang