我想加载包含大量 sql 查询(插入)的文本文件并使用 Python(PYODBC)执行它
我当前的代码如下所示:
cursor = cnxn.cursor()
with open('C:\Python_Script_Test\INSERTS.txt','r') as file:
var1 = file.read().replace("\r\n","")
var2 = var1
cursor.execute(var2)
file.close()
该文件中有超过 5000 行,其中的示例 INSERT 如下所示:
Insert into x (q, w, e, r, t, y, u, i, o, p, a, s, d, f, g, h, j)
VALUES (582, 'GA26', 'TMO ', 'xxx', 'xxx@example.com', '', 'NULL', 'NULL',
'', '', '', '', '', '', '', '', NULL);
错误:
pyodbc.ProgrammingError: ('42000',
"[42000] [MySQL][ODBC 3.51 Driver][mysqld-5.6.13]
You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near
'insert into x
(z, b, c, d, e' at line 3 (1064) (SQLExecDirectW)")
编辑:
另一个想法:
好的,目前没有错误。当前代码如下所示:previous = '' for sql_insert in open('C:\Python_Script_Test\x.txt','r'): y = sql_insert.strip().replace("\r\n",";" )
if y.startswith('insert'):
print previous if previous.endswith(';'): print 'test\r\n' cursor.execute(previous)
previous = y
else: previous += y
但是,数据库中的表没有变化......