當我嘗試使用%的語法與SQLAlchemy的和SQLite我得到一個錯誤。 PostgreSQL的語法相同工作正常:問題與%s與SQLAlchemy的核心和SQLite
import sqlalchemy
e = sqlalchemy.create_engine('sqlite:////tmp/x.db?timeout=120000')
e.execute('select * from people where name = %s;', 'joe').fetchall()
我得到這個:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 1396, in _cursor_execute
context)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/default.py", line 301, in do_execute
cursor.execute(statement, parameters)
sqlite3.OperationalError: near "%": syntax error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 1790, in execute
return connection.execute(statement, *multiparams, **params)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 1191, in execute
params)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 1287, in _execute_text
return self.__execute_context(context)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 1302, in __execute_context
context.parameters[0], context=context)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 1403, in _cursor_execute
context)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 1360, in _handle_dbapi_exception
from e
sqlalchemy.exc.OperationalError: (OperationalError) near "%": syntax error 'select * from people where name = %s;' ('joe',)
>>>
再次,Postgres的同樣的事情返回一個列表或行。 這是一個錯誤? 還是預計?
是的,沒有這個,字符串替換不會發生在Python中。 –
不,它不起作用。我沒有試圖使用python替代,但sqlalchemy語法。它適用於postgresql,但不適用於sqlite。 –
@ user1014841:對於sqlalchemy,您需要將'%'標誌轉義爲'%%' – avasal