【问题标题】:python _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now")python _mysql_exceptions.ProgrammingError:(2014,“命令不同步;您现在无法运行此命令”)
【发布时间】:2013-12-27 11:34:54
【问题描述】:

我正在尝试在 centos 机器上运行现有项目。我从 requirements.txt 安装了软件包,并且安装成功。但是在运行“python manage.py runserver”时,我得到了以下回溯。

[root@localhost bv]# python manage.py runserver
Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x98da9ec>>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
    self.validate(display_num_errors=True)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
    connection.validation.validate_field(e, opts, f)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
    db_version = self.connection.get_server_version()
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 415, in get_server_version
    self.cursor().close()
  File "/usr/local/lib/python2.7/site-packages/debug_toolbar/utils/tracking/__init__.py", line 9, in wrapped
    return callback(original, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/debug_toolbar/panels/sql.py", line 21, in cursor
    result = original(self)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 306, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
    self.connection = Database.connect(**kwargs)
  File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg/MySQLdb/connections.py", line 215, in __init__
    self.set_character_set(charset)
  File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg/MySQLdb/connections.py", line 294, in set_character_set
    super(Connection, self).set_character_set(charset)
_mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now")

需要帮助来解决这个问题。 谢谢

【问题讨论】:

  • 重新安装python-mysql
  • @NileshG 为什么要重新安装 python-mysql。 Mysql 配置正确,我可以通过命令提示符查看数据库。我安装了 mysql 连接器 1.2.3。更多我分别安装了 mysql 和 python。
  • 如果我也运行syncdb,我会得到同样的错误

标签: python django python-2.7 python-3.x django-models


【解决方案1】:

如果要执行多条sql语句,请不要使用cur.execute(sql)

更好的方法是使用cur.executemany(sql)

exp 1:这会导致一些错误

sql = "insert into test(name, age) value('Joe', '28');insert into test(name, age) value('xxtime', '15');"
    cursor = db.cursor()
    cursor.execute(sql)
    db.commit()

exp 2: 你可以用它来代替

  sql = "insert into test(name, age) value(%s, %s);"
    cursor = db.cursor()
    cursor.executemany(sql,[('Joe', '28'),('xxtime', '15')])
    db.commit()

【讨论】:

    【解决方案2】:

    您可以尝试在现有数据库上运行 -

    python manage.py inspectdb
    

    这将输出新的models.py,现在将新生成的models.py与你的models.py进行比较

    【讨论】:

    • 我现在仔细检查了。我在运行此命令时遇到了同样的错误。需要帮助解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2012-12-01
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多