【问题标题】:Getting a SQL error when running "python manage.py migrate" using a MySQL database使用 MySQL 数据库运行“python manage.py migrate”时出现 SQL 错误
【发布时间】:2015-11-25 01:32:30
【问题描述】:

我已经按照说明成功使用 SQLite 完成了 Django 的介绍性教程。但是,对于我的真实项目,我想使用 MySQL,所以我删除了教程项目文件并开始了新的设置。问题是当我运行“python manage.py migrate”时,我得到以下 MySQL 错误:

_mysql_exceptions.ProgrammingError: (1064, "您的 SQL 语法有错误;请查看您的 MySQL 服务器对应的手册 在第 1 行的 '%s' 附近使用正确语法的版本)。

我在站点设置数据库部分中有以下内容:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'vocabulator$database',
        'USER': 'vocabulator',
        'PASSWORD': '<password>',
        'HOST': 'mysql.server', 
    }
} 

我在 Bash 中使用 'mysql --user=vocabulator --host=mysql.server --password= "vocabulator\$database" '成功连接到这个数据库,所以看来我已经输入了这些设置细节正确。

我还遵循了为 Python 3 设置数据库绑定的说明,这似乎也可以成功运行。它位于 Django 文档页面或 PythonAnywhere 等效页面上,但遗憾的是我无法再次找到该页面引用。

我自己还没有编写任何 MySQL 查询,所以任何不正确的查询肯定来自 manage.py,那么可能是什么原因?下面引用了完整的错误跟踪:

Operations to perform:   Synchronize unmigrated apps: staticfiles, messages   Apply all migrations: contenttypes, sessions, admin, auth Synchronizing apps without migrations:   Creating tables...
    Running deferred SQL...   Installing custom SQL... Running migrations:   Rendering model states... DONE   Applying contenttypes.0001_initial...Traceback (most recent call last):   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 184, in execute
    self.errorhandler(self, exc, value)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/connections.py", line 37, in defaulterrorhandler
    raise errorvalue   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 171, in execute
    r = self._query(query)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 330, in _query
    rowcount = self._do_query(q)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 294, in _do_query
    db.query(q)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):   File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
    schema_editor.create_model(model)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 289, in create_model
    self.deferred_sql.extend(self._model_indexes_sql(model))   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/mysql/schema.py", line 55, in _model_indexes_sql
    self.connection.cursor(), model._meta.db_table   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/mysql/introspection.py", line 142, in get_storage_engine
    "WHERE table_name = %s", [table_name])   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 184, in execute
    self.errorhandler(self, exc, value)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/connections.py", line 37, in defaulterrorhandler
    raise errorvalue   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 171, in execute
    r = self._query(query)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 330, in _query
    rowcount = self._do_query(q)   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 294, in _do_query
    db.query(q) django.db.utils.ProgrammingError: (1064, "You have an error in your  SQL syntax; check the manual that corresponds to your MySQL server v ersion for the right syntax to use near '%s' at line 1") Exception ignored in: <bound method Cursor.__del__ of <MySQLdb.curso rs.Cursor object at 0x7feec394b940>> Traceback (most recent call last):   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-p ackages/MySQLdb/cursors.py", line 67, in __del__   File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-p ackages/MySQLdb/cursors.py", line 73, in close ReferenceError: weakly-referenced object no longer exists (django18)03:29 ~/mysite $ mysql --user=vocabulator --host=mysql.server --p mysql: ambiguous option '--p' (pager, plugin_dir)

【问题讨论】:

  • 你有 --password= "vocabulator\$database" ',但在你的 django 设置中这是数据库名称?
  • 我使用了被解释为 HTML 标记的 符号,这在我的帖子中造成了错字。我现在已经修好了。如您所说,最后一部分是数据库名称。我显然没有在帖子中引用实际密码。
  • 你使用的是什么 MySQL 驱动程序?
  • 您的问题使我进入了安装连接器所遵循的页面(我在帖子中称它们为绑定):pythonanywhere.com/wiki/UsingMySQL 我安装的驱动程序是该页面上列出的驱动程序,这似乎是与 MySQL 5.5 和 Python 3.4 兼容。但是,我现在发现我稍微偏离了这些说明,但是当我更改我的设置文件以使其完全匹配时,会出现新的错误。当我按照说明将 'ENGINE' 更改为 'mysql.connector.django' 时,错误变为“ImportError: cannot import name 'BaseDatabaseFeatures'”。
  • 事实上,其他人似乎遇到了我的确切情况并在三个月前发布在 Stackoverflow 上。 stackoverflow.com/questions/30426512/…我在底部看到有人对此的回应,所以我会调查那个。

标签: python django python-3.x pythonanywhere


【解决方案1】:

我试过了,效果很好……

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database',
        'USER': 'vocabulator',
        'PASSWORD': '<password>',
        'HOST': 'mysql.server', 
    }
}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

正如我在上面的评论中所说,我找到了指导我如何安装驱动程序的页面。就是这个:

https://www.pythonanywhere.com/wiki/UsingMySQL

然而,这个 Wiki 页面现在有点过时了,它指示我安装 MySQL 连接器版本 2.0.1。当前版本现在是 2.0.4。旧版本有一个错误,这意味着它不能在 Django 1.8 上运行,如下所述:

http://bugs.mysql.com/bug.php?id=76752

到 2.0.4 版时,此错误似乎已得到纠正。

上面引用的 PythonAnywhere Wiki 页面中的行应更改为:

pip3.4 安装https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.4.tar.gz

运行此程序后,迁移似乎已在我的 Django 安装上成功运行。

【讨论】:

  • PythonAnywhere 开发人员:感谢您的提醒!我们已经修复了帮助页面。
  • @Terry Rozmus 我在 python 3.4 django 1.8 windows 8 mysql 数据库中遇到了同样的错误。我已经尝试了很多东西。还安装了 mysql-connector-python-2.1.3.tar.gz。请帮忙
  • 除非您发布遇到的错误,否则很难知道您面临什么问题。另外我是 Django 初学者。我很擅长解决故障,但您可能需要更有经验的人的帮助。我建议您将您的查询作为一个新的 stackoverflow 问题发布,并附上您的错误的详细概述以及您尝试过的内容,以便您获得此帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
相关资源
最近更新 更多