2014-11-03 94 views
0

请原谅我,我是相当新的python,我一直在寻找这段的逻辑很长一段时间,但是,不管我尝试什么,看起来它总是在打印语句中崩溃。基本上,我只想知道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 
+0

我猜'row'不是一个字符串,所以'row.split'看起来不对...... – 2014-11-03 19:11:33

+1

回溯清楚地表明,当你调用'self.rollback()',甚至没有出现错误时在你提供的代码中。而且,如果你没有显示你正在运行的命令,你如何期望我们调试你的命令不同步的方式? – 2014-11-03 19:11:41

+0

请显示完整的回溯 - 从您提供的错误信息看,它看起来不像'print value'行是什么引起了异常。 – martineau 2014-11-03 19:14:29

回答

0

当您退出withconnection.__exit__被调用,而不是在print语句您的错误发生。 当你看的MySQLdb的这部分代码,你会看到:

def __enter__(self): return self.cursor() 

def __exit__(self, exc, value, tb): 
    if exc: 
     self.rollback() 
    else: 
     self.commit() 

所以这立刻告诉我们两两件事:

  1. 有前一个例外是由rollback()调用自身原因造成的阴影例外
  2. 你的第一行应该是)with conn as cur:,因为cur将被赋予connection.__enter__()的结果。

很难说为什么你会得到这个错误,因为我们不知道你的光标从哪里来。你应该改变你的第一行,如上所述,它可能会工作。如果没有,完全摆脱上下文管理器,您将能够看到原始异常。

相关问题