python _mysql_exceptions.ProgrammingError:(2014,“命令不同步;你现在不能运行此命令”)

时间:2013-12-10 04:37:22

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

我正在尝试在centos machine中运行现有项目。我从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")

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

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:-1)

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

python manage.py inspectdb

这将输出新的models.py,现在将新生成的models.py与models.py

进行比较