0

我正在尝试将 CSV 文件和文本文件中的数据插入 SQL SERVER SSMS 18.7 版。下面是我的代码。

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")

当我运行查询时,我收到以下错误。

 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

如果我使用文本文件,有人可以帮助我们解决这个问题吗?

4

1 回答 1

0

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

于 2021-02-22T00:43:46.630 回答