0

I am trying to insert data from a CSV file and also from a textfile into SQL SERVER SSMS version 18.7. Below is my code.

import pyodbc
import csv
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=????;'
                      'Database=???;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()

with open('C:/python/names.txt', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        cursor.execute("""INSERT INTO TESTPYTHON (id,name) VALUES(%s,%s)""", row)

conn.commit()
cursor.close()
print ("Done")

When I run the query I get the following error.

 cursor.execute("""INSERT INTO TESTPYTHON (id,name) VALUES(%s,%s)""", row)
pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000')

Process finished with exit code 1

Can someone help we with this also how should I separate the values if I am using text files?

MattDMo
  • 100,794
  • 21
  • 241
  • 231

1 Answers1

0

Maybe try using the unload operator, like cursor.execute("""INSERT INTO TESTPYTHON (id,name) VALUES(%s,%s)""", *row)

thebadgateway
  • 433
  • 1
  • 4
  • 7