import pyodbc
import csv
conn = pyodbc.connect('Driver=ODBC Driver 17 for SQL Server;'
'Server=SIS10647MSSQLSERVER14;'
'Database=LeelaVenkatesh;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE employee
(
empid int,
Name nvarchar(50)
)
''')
with open('C:PYTHONTextfiles/1.txt') as f:
reader = csv.reader(f)
for row in reader:
cursor.execute("""INSERT INTO employee(empid,name)
VALUES(%s, %s)
""", row)
conn.commit()
cursor.close()
print("Done")
我得到以下错误
cursor.execute("&"INSERT INTO employee(empid,name(pyodbc。编程错误:('SQL包含0个参数标记,但包含1提供了参数","HY000"(
文本文件数据看起来像
empid|Name
1|'Ram'
cursor.execute("""INSERT INTO employee(empid,name)
VALUES(?, ?)
""", row)
如果这不起作用,假设行是一个数组,试试这个:
cursor.execute("""INSERT INTO employee(empid,name)
VALUES(?, ?)
""", row[0], row[1])
empid|Name
1|'Ram'
try this
empid,Name
1,Ram
with
cursor.execute("""INSERT INTO employee(empid,name)
VALUES(?, ?)
""", row[0], row[1])