I have a large CSV file and I want to insert it all at once, instead of row by row. This is my code:
import pypyodbc
import csv
con = pypyodbc.connect('driver={SQL Server};' 'server=server_name;' 'database=DB-name;' 'trusted_connection=true')
cur = con.cursor()
csfile = open('out2.csv','r')
csv_data = csv.reader(csfile)
for row in csv_data:
try:
cur.execute("BULK INSERT INTO Table_name(Attribute, error, msg, Value, Success, TotalCount, SerialNo)" "VALUES (?, ?, ?, ?, ?, ?, ?)", row)
except Exception:
time.sleep(60)
cur.close()
con.commit()
con.close()