Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 其中()未按sqlalchemy中的预期处理参数_Python_Sql_Sqlite_Sqlalchemy - Fatal编程技术网

Python 其中()未按sqlalchemy中的预期处理参数

Python 其中()未按sqlalchemy中的预期处理参数,python,sql,sqlite,sqlalchemy,Python,Sql,Sqlite,Sqlalchemy,这将获得正确的表,但由于某些原因,所构造的查询不是预期的查询。下面是构造的查询 table = engine.getFTable(); dBtable = table; query = select([dBtable.c.id]).where( dBtable.c.FName == 'F1' ); print(query); 而不是 SELECT "FList".id FROM "FList" WHERE "FList"."FName" = ? SQLAlchemy尽可能地创造 这意味

这将获得正确的表,但由于某些原因,所构造的查询不是预期的查询。下面是构造的查询

table = engine.getFTable();
dBtable = table; 
query = select([dBtable.c.id]).where( dBtable.c.FName == 'F1' );
print(query);
而不是

SELECT "FList".id 
FROM "FList" 
WHERE "FList"."FName" = ?
SQLAlchemy尽可能地创造

这意味着查询中的
,其中
子句只是一个占位符,在执行查询时将用实际数据(
'F1'
)填充(然后将参数绑定到查询)

确切的数字取决于您使用的SQL方言(PostgreSQL、MySQL等等)。有些使用
,有些支持命名参数,如
:user\u id

您可以通过编译该查询对象并显示其参数来查看将要填写的参数:

SELECT "FList".id 
FROM "FList" 
WHERE "FList"."FName" = 'F1'
另请注意该文件中的一段:

上述表单将在将SQL语句传递给 PythonDBAPI,其中包括不呈现绑定参数 内联。SQLAlchemy通常不会将绑定参数字符串化,如 这是由Python DBAPI适当处理的,更不用说了 绕过边界参数可能是最广泛利用的方法 现代web应用程序中的安全漏洞

也就是说,参数化查询以及单独绑定到它的参数将像这样传递给Python DBAPI(如果愿意的话,DB“driver”),因为这是最高效、最安全的方法。您可以使用上面所示的内联参数来可视化查询,但这并不是真正要实现的


相反,如果您正在调试查询,则更应该使用:这将方便地在执行参数化查询时记录查询及其绑定参数

>>> query = select([table.c.userid]).where(table.c.userid == 'lukas.graf')
>>> print query
SELECT users.userid
FROM users
WHERE users.userid = :userid_1            <---- bind parameter placeholder

>>> compiled = query.compile()
>>> print compiled.params
{u'userid_1': 'lukas.graf'}
>>> from sqlalchemy.dialects import postgresql
>>>
>>> query = select([table.c.userid]).where(table.c.userid == 'lukas.graf')
>>> compiled = query.compile(dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True})
>>> print compiled
SELECT users.userid
FROM users
WHERE users.userid = 'lukas.graf'