【问题标题】:Python MySQLdb handle multiple cursors: Command out of syncPython MySQLdb处理多个游标:命令不同步
【发布时间】:2013-10-18 06:54:26
【问题描述】:

我有一个数据库连接方法,它创建连接并将光标设置为 init 进程的一部分。

然后我有以下使用光标的方法:

def calculatePercentile(self):
    user_edits = ur'''SELECT /* SLOW_OK_LIMIT: 1800 */ user_id, user_editcount from user'''
    num_user_per_percentile = ur'''SELECT /* SLOW_OK_LIMIT: 1800 */ count(user_id) from user where user_editcount = %(count)s'''        
    lang_edit_count_dictionary = {}
    lang_edit_count_dictionary[self.language] = []
    edit_count_list = []
    p = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95]
    bot_users = self._getBotUsers()
    success = False
    attempts = 0
    while attempts < 3 and not success:
        try:
            self.dbCursor.execute(user_edits)
            for user in self.dbCursor.fetchall():
                user_id = user['user_id']
                user_editcount = user['user_editcount']
                if user_id not in bot_users:
                    edit_count_list.append(user_editcount)
            edit_count_list.sort()
            for i in p:
                lang_edit_count_dictionary[self.language].append(np.percentile(edit_count_list, i))
            success = True
        except MySQLdb.OperationalError, sqlEx:
            attempts += 1
            if sqlEx[0] == 2006:
                logging.info("Caught the MySQL server gone away exception")
                logging.error(sqlEx)
                time.sleep(10)
                self.connectServer()
        except Exception, e:
            traceback.print_exc()
            logging.exception(e)
    for key, values in lang_edit_count_dictionary.iteritems():
        print key
        for value in values:
            self.dbCursor.execute(num_user_per_percentile, {"count":value})
            uEditCount = self.dbCursor.fetchone()
            print uEditCount

此方法的作用是,它执行一个查询,获取其数据并将该数据转储到其中,然后使用相同的游标在其中执行另一个查询:

for key, values in lang_edit_count_dictionary.iteritems():
    print key
    for value in values:
        self.dbCursor.execute(num_user_per_percentile, {"count":value})
        uEditCount = self.dbCursor.fetchone()
        print uEditCount

发生的情况是我收到以下错误:

    self.dbCursor.execute(num_user_per_percentile, {"count":value})
  File "/home/auduwage/code/vInterLang/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 201, in execute
    self.errorhandler(self, exc, value)
  File "/home/auduwage/code/vInterLang/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
_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 SSDictCursor.__del__ of <MySQLdb.cursors.SSDictCursor object at 0x2716ed0>> ignored

在此之前,我在不同的方法中使用了相同的游标,我认为这与 MySql 不允许我使用相同的游标一个接一个地运行多个查询无关?或者是吗? 解决办法是什么?

【问题讨论】:

    标签: python mysql cursor mysql-python


    【解决方案1】:

    您不能运行复合 sql 语句。 你需要把它们分开。

    这将返回该错误:

    cursor.execute("drop database x; drop table y;")
    

    这样可以正常工作:

    cursor.execute("drop database x;")
    cursor.execute("drop table y;")
    

    【讨论】:

      猜你喜欢
      • 2013-12-03
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 2016-05-28
      相关资源
      最近更新 更多