代码如下
[Python] 纯文本查看 复制代码 #coding=utf-8
import pymssql
import xlrd
data = xlrd.open_workbook('fenlei.xls')
table = data.sheet_by_name('Sheet1')
nrows =table.nrows
conn = pymssql.connect(host='localhost', server='KDA', user='sa', password='', database='test', port='1433')
cur = conn.cursor()
for i in range(1, nrows):
x = table.row_values(i)
id = x[0]
ia = x[1]
cur.execute("INSERT INTO Table_1(test,test1) VALUES (id, ia)")
conn.commit()
cur.close()
conn.close()
报错信息
[Python] 纯文本查看 复制代码 C:/Users/admin/PycharmProjects/untitled/car.py:2: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working
import pymssql
Traceback (most recent call last):
File "src\pymssql.pyx", line 448, in pymssql.Cursor.execute
File "src\_mssql.pyx", line 1064, in _mssql.MSSQLConnection.execute_query
File "src\_mssql.pyx", line 1095, in _mssql.MSSQLConnection.execute_query
File "src\_mssql.pyx", line 1228, in _mssql.MSSQLConnection.format_and_run_query
File "src\_mssql.pyx", line 1639, in _mssql.check_cancel_and_raise
File "src\_mssql.pyx", line 1683, in _mssql.maybe_raise_MSSQLDatabaseException
_mssql.MSSQLDatabaseException: (128, b'The name "id" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/admin/PycharmProjects/untitled/car.py", line 17, in <module>
cur.execute("INSERT INTO Table_1(test,test1) VALUES (id, ia)")
File "src\pymssql.pyx", line 468, in pymssql.Cursor.execute
pymssql.OperationalError: (128, b'The name "id" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n')
单元格能遍历到数据,写入数据库也能写 但是通过Excel导入的这个逻辑 还没弄清楚。
希望大佬们能给指点一下 |