I use SQLAlchemy declarative to work with MySQL.
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relation, sessionmaker
import datetime
engine = create_engine('mysql+mysqldb://root:mypass@localhost/somedb')
Base = declarative_base()
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
class Item(Base):
__tablename__ = 'item'
id = Column(Integer, primary_key=True)
dt = Column(DateTime)
title = Column(UnicodeText)
Base.metadata.create_all(engine)
i = Item()
i.dt = datetime.datetime.now()
i.title = "hello world"
session.add(i)
session.commit()
This code throws an exception on session.commit()
:
sqlalchemy.exc.ProgrammingError: (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, %s)' at line 1") b'INSERT INTO item (dt, title) VALUES (%s, %s)' (datetime.datetime(2014, 5, 16, 20, 58, 7, 729245), 'hello world')
The full stack trace of exception is in the end of the post.
I have found out that the parameters are not being inserted into the query string properly. There is as a line query = query.format( *db.literal(args) ) in the file MySQLdb/cursors.py, on the line 163
. The query
variable is a string. The string.format
function accepts replacement fields like {}
, {1}
, {23}
, not %s
. But the replacement fields in the query variable are %s
.
So, parameters are not being inserted into the query.
I could solve the problem by making changes in the sqlalchemy/sql/compiler.py
file, variable BIND_TEMPLATES
. I have changed 'format': "%%s"
to 'format': "{}"
.
But I clearly understand that people use successfully sqlalchemy without this error. What is the real cause of the problem? Maybe I have installed some incompatible versions?
I use Python 3.4, on Windows 7 x64, Mysql connector: MySQL_python-1.2.3-py3.4-win-amd64.egg
The full stack trace of the exception:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.1.3\helpers\pydev\pydevd.py", line 1539, in <module>
debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.1.3\helpers\pydev\pydevd.py", line 1150, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.1.3\helpers\pydev\_pydev_execfile.py", line 37, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc) #execute the script
File "C:/Work/midmay/midmay.parser/scripts/test.py", line 30, in <module>
session.commit()
File "C:\Python34\lib\site-packages\sqlalchemy\orm\session.py", line 765, in commit
self.transaction.commit()
File "C:\Python34\lib\site-packages\sqlalchemy\orm\session.py", line 370, in commit
self._prepare_impl()
File "C:\Python34\lib\site-packages\sqlalchemy\orm\session.py", line 350, in _prepare_impl
self.session.flush()
File "C:\Python34\lib\site-packages\sqlalchemy\orm\session.py", line 1903, in flush
self._flush(objects)
File "C:\Python34\lib\site-packages\sqlalchemy\orm\session.py", line 2021, in _flush
transaction.rollback(_capture_exception=True)
File "C:\Python34\lib\site-packages\sqlalchemy\util\langhelpers.py", line 57, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Python34\lib\site-packages\sqlalchemy\util\compat.py", line 168, in reraise
raise value
File "C:\Python34\lib\site-packages\sqlalchemy\orm\session.py", line 1985, in _flush
flush_context.execute()
File "C:\Python34\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 370, in execute
rec.execute(self)
File "C:\Python34\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 523, in execute
uow
File "C:\Python34\lib\site-packages\sqlalchemy\orm\persistence.py", line 64, in save_obj
mapper, table, insert)
File "C:\Python34\lib\site-packages\sqlalchemy\orm\persistence.py", line 594, in _emit_insert_statements
execute(statement, params)
File "C:\Python34\lib\site-packages\sqlalchemy\engine\base.py", line 720, in execute
return meth(self, multiparams, params)
File "C:\Python34\lib\site-packages\sqlalchemy\sql\elements.py", line 317, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Python34\lib\site-packages\sqlalchemy\engine\base.py", line 817, in _execute_clauseelement
compiled_sql, distilled_params
File "C:\Python34\lib\site-packages\sqlalchemy\engine\base.py", line 947, in _execute_context
context)
File "C:\Python34\lib\site-packages\sqlalchemy\engine\base.py", line 1108, in _handle_dbapi_exception
exc_info
File "C:\Python34\lib\site-packages\sqlalchemy\util\compat.py", line 174, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=exc_value)
File "C:\Python34\lib\site-packages\sqlalchemy\util\compat.py", line 167, in reraise
raise value.with_traceback(tb)
File "C:\Python34\lib\site-packages\sqlalchemy\engine\base.py", line 940, in _execute_context
context)
File "C:\Python34\lib\site-packages\sqlalchemy\engine\default.py", line 435, in do_execute
cursor.execute(statement, parameters)
File "C:\Python34\lib\site-packages\mysql_python-1.2.3-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 184, in execute
self.errorhandler(self, exc, value)
File "C:\Python34\lib\site-packages\mysql_python-1.2.3-py3.4-win-amd64.egg\MySQLdb\connections.py", line 37, in defaulterrorhandler
raise errorvalue
File "C:\Python34\lib\site-packages\mysql_python-1.2.3-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 171, in execute
r = self._query(query)
File "C:\Python34\lib\site-packages\mysql_python-1.2.3-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 330, in _query
rowcount = self._do_query(q)
File "C:\Python34\lib\site-packages\mysql_python-1.2.3-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 294, in _do_query
db.query(q)
sqlalchemy.exc.ProgrammingError: (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, %s)' at line 1") b'INSERT INTO item (dt, title) VALUES (%s, %s)' (datetime.datetime(2014, 5, 16, 20, 58, 7, 729245), 'hello world')