首页 > 解决方案 > SQLAlchemy 文本参数绑定给出“?”

问题描述

我正在为 SQL Server 使用 ODBC 驱动程序 17

我有这个:

q = text('select top 10 * from :x')

conn.execute(q, x="mytable")

返回失败:

sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Must declare the table variable "@P1". (1087) (SQLExecDirectW)')
[SQL: select top 10 * from ?]
[parameters: ('mytable',)]

然而,跑步q = test('select top 10 * from mytable')有效。

正如我所尝试的那样,我迷失了方向bindparams

标签: pythonsqlalchemy

解决方案


马蒂金的评论是正确的。绑定参数仅适用于数据。

q = text('select top 10 * from :x')

conn.execute(q, x="mytable")

是无效的。

但是这个,

q = text('select :x from mytable')

conn.execute(q, x="thing1")

作品。


推荐阅读