【发布时间】:2014-11-03 18:51:55
【问题描述】:
请原谅我,我对 python 还很陌生,而且我已经研究这个部分的逻辑很长一段时间了,但是,无论我尝试什么,它似乎总是在 print 语句中崩溃.基本上,我只是想知道 python 是否从 SQL 语句中获取了正确的值。我什至尝试过做一个
a,b,c = row.split(',') 然后是 print a 但它也会在打印时出错。
with con:
cur.execute(query, (next, end,next,end))
print (cur._last_executed) # this prints the correct query.
while True:
result = cur.fetchmany()
if len(result) ==0:
break
for row in result:
myvalues = row.split(',')
for value in myvalues:
print value # this line is what the traceback says caused it.
错误输出:
Traceback (most recent call last):
File "./export.py", line 55, in <module>
print value
File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/MySQLdb/connections.py", line 249, in __exit__
self.rollback()
_mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now")
Exception _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") in <bound method SSCursor.__del__ of <MySQLdb.cursors.SSCursor object at 0x7fc0e0632f10>> ignored
【问题讨论】:
-
我猜
row不是字符串,所以row.split看起来不对... -
回溯清楚地表明当您调用
self.rollback()时发生了错误,您提供的代码中甚至都没有出现。此外,如果您不显示正在运行的命令,您希望我们如何调试您的命令如何不同步? -
请显示完整的回溯——从您提供的错误信息来看,
print value行似乎不是导致异常的原因。 -
当您退出上下文管理器时实际上会发生错误 - 您通过执行
with con:进入的那个。连接开始关闭,发出rollback,触发错误。不知道为什么会这样。您是否尝试在with块的末尾调用cur.close()? -
当您退出上下文管理器(
with块)时发生错误,而不是在print语句处。我的猜测是您的 SQL 命令与 Python DB API 的命令冲突。我特别猜测您的查询中可能有"COMMIT;",这可能会导致此错误(如this related question 所示)。
标签: python