【问题标题】:issue with %s with sqlalchemy core and sqlitesqlalchemy 核心和 sqlite 的 %s 问题
【发布时间】:2012-12-06 12:55:44
【问题描述】:

当我尝试将 %s 语法与 sqlalchemy 和 sqlite 一起使用时,出现错误。与 postgresql 相同的语法可以正常工作:

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


    【解决方案1】:

    'select * from people where name = %s;', 'joe'中的字符串格式化有一个小问题

    %s 用于字符串格式化,%s 期望字符串后有一个 % some sort Data to be replaced

    您应该从%s;', 'joe' 中删除, 并添加% 以获取

    In [10]: 'select * from people where name = %s;' %'joe'
    Out[10]: 'select * from people where name = joe;'
    

    【讨论】:

    • 是的,没有它,python 中不会发生字符串替换。
    • 不,它不起作用。我不是在尝试使用 python 替换,而是使用 sqlalchemy 语法。它适用于 postgresql,但不适用于 sqlite。
    • @user1014841:对于 sqlalchemy,您需要将 % 符号转义为 %%
    【解决方案2】:

    van 在他对another of my posts 的回答中给出了正确答案。解决方案是使用sqlalchemy "text"。这适用于 SQLite 和 postgreSQL(我假设其他服务器也是如此),因此您可以编写一次并针对不同的数据库服务器使用它(当然,只要您保持 sql 通用)。

    【讨论】:

      【解决方案3】:

      我通过使用问号语法解决了这个问题:

      e.execute('select * from people where name = ?;', 'joe').fetchall()
      

      这适用于 sqlite 和 postgres

      来自the chapter about sqlalchemy in the Pylon's book

      不同的数据库使用不同的标记(称为参数样式)来标记您传递给 execute() 的变量应该插入的位置。上面的例子使用了 SQLite,它使用 ?作为参数样式,但如果您尝试使用 MySQL 或 PostgreSQL,则需要使用 %s 作为参数样式。 SQL 将如下所示

      正确的答案是用户 sqlalchemy "text"(见我的另一个答案)。

      【讨论】:

      • 这不是和你原来的帖子一样吗?你的意思是 ('select * from people where name = ?;' , 'joe') 可能吗?
      • 你是对的,我从我的原始帖子中复制并粘贴了它而没有修复它。我现在已经编辑了。
      猜你喜欢
      • 2011-08-21
      • 2010-10-09
      • 2014-09-18
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 2014-02-08
      相关资源
      最近更新 更多