从字符串构建 sqlalchemy 查询
Build sqlalchemy query from string
如何将 sql where 子句字符串转换为 sqlalchemy 查询?我假设我已经知道 table。
我正在构建一个 Angular 网络应用程序,它点击 Flask API 获取数据。 Flask 正在使用 sqlalchemy 查询数据库。 jQuery-QueryBuilder fromSQL (http://querybuilder.js.org/plugins.html#import-export) 将过滤器导出为原始 SQL,我想将其传回 api、解析和查询。
例如:
where_str = "name LIKE '%BOB%' AND fruit != 'pineapple'"
将转换为:
session.query(Table).filter(Table.name.like('%BOB%')).filter(Table.fruit != 'pineapple)
标记 blaze 因为 odo 可能是我需要的。
您无需过滤两次即可添加 AND 您可以这样做:
session.query(Table).filter(Table.name.like('%BOB%'), Table.fruit != 'pineapple)
您可以尝试执行session.query(Table).filter(where_str)
。它适用于 SQLAlchemy v0.5.8。
您还可以构建整个 SQL 语句字符串并使用 Connection.execute() method to execute it. Either way, passing SQL statements as strings directly from the webpage to the application can be very dangerous.
如何将 sql where 子句字符串转换为 sqlalchemy 查询?我假设我已经知道 table。
我正在构建一个 Angular 网络应用程序,它点击 Flask API 获取数据。 Flask 正在使用 sqlalchemy 查询数据库。 jQuery-QueryBuilder fromSQL (http://querybuilder.js.org/plugins.html#import-export) 将过滤器导出为原始 SQL,我想将其传回 api、解析和查询。
例如:
where_str = "name LIKE '%BOB%' AND fruit != 'pineapple'"
将转换为:
session.query(Table).filter(Table.name.like('%BOB%')).filter(Table.fruit != 'pineapple)
标记 blaze 因为 odo 可能是我需要的。
您无需过滤两次即可添加 AND 您可以这样做:
session.query(Table).filter(Table.name.like('%BOB%'), Table.fruit != 'pineapple)
您可以尝试执行session.query(Table).filter(where_str)
。它适用于 SQLAlchemy v0.5.8。
您还可以构建整个 SQL 语句字符串并使用 Connection.execute() method to execute it. Either way, passing SQL statements as strings directly from the webpage to the application can be very dangerous.