【发布时间】:2017-02-15 09:09:17
【问题描述】:
我在将 CSV 文件上传到 MS SQL Server 中的表时遇到问题,CSV 文件有 25 列,并且标题与 SQL 中的表同名,SQL 中的表也有 25 列。当我运行脚本时,它会引发错误
params arg (<class 'list'>) can be only a tuple or a dictionary
将此数据导入 MS SQL 的最佳方法是什么? CSV 和 SQL 表都具有完全相同的列名。
代码如下:
import csv
import pymssql
conn = pymssql.connect(
server="xx.xxx.xx.90",
port = 2433,
user='SQLAdmin',
password='xxxxxxxx',
database='NasrWeb'
)
cursor = conn.cursor()
customer_data = csv.reader('cleanNVG.csv') #25 columns with same header as SQL
for row in customer_data:
cursor.execute('INSERT INTO zzzOracle_Extract([Customer Name]\
,[Customer #]\
,[Account Name]\
,[Identifying Address Flag]\
,[Address1]\
,[Address2]\
,[Address3]\
,[Address4]\
,[City]\
,[County]\
,[State]\
,[Postal Code]\
,[Country]\
,[Category ]\
,[Class]\
,[Reference]\
,[Party Status]\
,[Address Status]\
,[Site Status]\
,[Ship To or Bill To]\
,[Default Warehouse]\
,[Default Order Type]\
,[Default Shipping Method]\
,[Optifacts Customer Number]\
,[Salesperson])''VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,)',row)
conn.commit()
cursor.close()
print("Done")
conn.close()
这是 CSV 文件的第一行的样子
【问题讨论】:
-
能否显示 CSV 文件的第一行(或第一行的第一列...),以便我们查看它的样子?
-
刚刚截取了数据的样子。 @SergeBallesta
-
您应该复制CSV的文本,而不是截取Excel的屏幕截图,这样会删除文件的分隔符
标签: python python-3.x pymssql