【问题标题】:Python SQLAlchemy executes via MySQL terminal but not via engine.executePython SQLAlchemy 通过 MySQL 终端而不是通过 engine.execute 执行
【发布时间】:2015-10-31 14:32:56
【问题描述】:

你好,

我有一个非常简单的选择语句:

select order_id as oid2, ship_update_date, status_type
from tracking_update
where status_type like 'M'

我已将其转换为函数,原始 SQL 为:

for letter in status_filter:
    db.session.execute(
        'select order_id, ship_update_date, status_type '
        'from tracking_update where status_type = %s' % letter
)

但是,当通过 Python 执行此 SQL 时,我收到以下信息:

OperationalError: (_mysql_exceptions.OperationalError) (1054, "Unknown column 'P' in 'where clause'") [SQL: u'select order_id, ship_update_date, status_type from tracking_update where status_type = "P"']

明明是传信,为什么会把我的搜索值当作一列呢?

【问题讨论】:

    标签: python mysql sqlalchemy


    【解决方案1】:

    你好,

    我把代码改成如下:

    "select order_id, ship_update_date, status_type "
                        "from tracking_update where status_type=:param", {"param":letter}
    

    我的参数现在被正确传递。

    感谢 Session API 文档:

    SQL alchemy session documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2012-09-27
      相关资源
      最近更新 更多