python manage.py migrate使用django.db.utils.ProgrammingError返回:(1064 ...错误

时间:2015-11-21 13:22:03

标签: python mysql django

我是新手,我正在尝试使用mysql数据库使用Python 3和Django 1.8.6在Windows 8上创建一个django项目

当我执行python manage.py migrate时,我收到错误

django.db.utils.ProgrammingError: (1064 ....

I have cross checked my settings.py file and everything. In fact the connection is established and sometimes it successfully creates tables`django_content_type` and `django_migrations` and then gives this error and sometimes without creating these tables itself I get this error.

打印的堆栈跟踪结束如下:

django.db.utils.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")

我也提到并尝试按照这个站点上的其他类似查询中的说明(安装了mysql-connector-python-2.1.3),但似乎没有任何工作

完整的堆栈跟踪如下:

D:\pythonLearn\learningDjango\mysite>python manage.py migrate
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 64, in execute return self.cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\mysql\base.py", line 124, in execute return self.cursor.execute(query, args)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-md64.egg\MySQLdb\cursors.py", line 207, in execute self.errorhandler(self, exc, value)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-md64.egg\MySQLdb\connections.py", line 37, in defaulterrorhandler raise errorvalue
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-md64.egg\MySQLdb\cursors.py", line 192, in execute r = self._query(query)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-md64.egg\MySQLdb\cursors.py", line 356, in _query rowcount = self._do_query(q)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-md64.egg\MySQLdb\cursors.py", line 320, 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 "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 354, in execute_from_command_line utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 346, in execute self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 394, in run_from_argv self.execute(*args, **cmd_options)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 445, in execute output = self.handle(*args, **options)
File "C:\Python34\lib\site-packages\django\core\management\commands\migrate.py", line 93, in handle executor = MigrationExecutor(connection, self.migration_progress_callback)
File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 19, in __init__self.loader = MigrationLoader(self.connection)
File "C:\Python34\lib\site-packages\django\db\migrations\loader.py", line 47, in __init__self.build_graph()
File "C:\Python34\lib\site-packages\django\db\migrations\loader.py", line 188, in build_graph   self.applied_migrations = recorder.applied_migrations()
File "C:\Python34\lib\site-packages\django\db\migrations\recorder.py", line 59, in applied_migrations self.ensure_schema()
File "C:\Python34\lib\site-packages\django\db\migrations\recorder.py", line 53, in ensure_schema editor.create_model(self.Migration)
File "C:\Python34\lib\site-packages\django\db\backends\base\schema.py", line 289, in create_model self.deferred_sql.extend(self._model_indexes_sql(model))
File "C:\Python34\lib\site-packages\django\db\backends\mysql\schema.py", line 56, in _model_indexes_sql self.connection.cursor(), model._meta.db_table
File "C:\Python34\lib\site-packages\django\db\backends\mysql\introspection.py", line 142, in get_storage_engine
"WHERE table_name = %s", [table_name])
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 79, in execute return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 64, in execute return self.cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\utils.py", line 98, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python34\lib\site-packages\django\utils\six.py", line 658, in reraise raise value.with_traceback(tb)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 64, in execute return self.cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\mysql\base.py", line 124, in execute return self.cursor.execute(query, args)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 207, in execute self.errorhandler(self, exc, value)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-amd64.egg\MySQLdb\connections.py", line 37, in defaulterrorhandler raise errorvalue
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 192, in execute r = self._query(query)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 356, in _query rowcount = self._do_query(q)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 320, 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 version for the right syntax to use near '%s' at line 1")

3 个答案:

答案 0 :(得分:0)

您的错误消息显示......

  

...正确的语法使用近&#39;%s&#39;在第1行

这意味着程序传递给MySQL的查询包含文本字符串%s。在我看来,你的程序没有将值替换为该字符串。在尝试将字符串用作查询之前,必须这样做。

在查询中使用占位符的这种业务很常见。您的查询是否可以说出类似的内容?

 INSERT INTO names (name) VALUES (%s)

需要说明这一点

 INSERT INTO names (name) VALUES ('Ban Ki Moon')

答案 1 :(得分:0)

django.db.utils.ProgrammingError:(1064,“您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以获取在((6)NOT NULL)附近使用的正确语法)第1行”)

需要将mysql升级到5.6或更高版本。这是由于DATETIME(6)引起的。支持5.6或更高版本。

参考 https://docs.djangoproject.com/en/2.1/ref/databases/#fractional-seconds-support-for-time-and-datetime-fields

答案 2 :(得分:0)

正如@krishna chandak所说,由于MySQL,您应该升级DATETIME(6)版本。

如何更新MySQL版本:

确保yum是最新的

yum update yum

安装yum-utils(它提供yum-config-manager)

yum install yum-utils

备份:将所有数据库转储到all_dbs.sql

mysqldump --lock-all-tables --all-databases -uroot -yourpassword >/root/all_dbs.sql

停止MySQL守护程序

service mysqld stop

下载最新的MySQL

------对于CentOS 6

wget https://dev.mysql.com/get/mysql80-community-release-el6-2.noarch.rpm
sudo rpm -Uvh mysql80-community-release-el6-2.noarch.rpm

------对于CentOS 7

wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm

删除旧版本并安装5.7版本

sudo yum-config-manager --enable mysql57-community
sudo yum-config-manager --disable mysql80-community
yum remove mysql55-server
yum install mysql57-server
yum update mysql

启动MySQL

service mysqld start

如果遇到任何错误,则必须删除etc / my.cnf中不推荐使用的变量

升级现有数据库

mysql_upgrade -uroot -p

如果您* .sock发生错误

通过添加更改配置/etc/my.cnf

datadir=/var/lib/mysql

并注释该字符串

# socket=/var/lib/mysql/mysql.sock

然后执行更新数据库并将配置返回到先前状态的步骤。