sta*_*123 193 python sql sqlalchemy flask flask-sqlalchemy
如何在SQLAlchemy中执行原始SQL?
我有一个python web应用程序,它运行在烧瓶上,并通过SQLAlchemy与数据库连接.
我需要一种方法来运行原始SQL.该查询涉及多个表连接以及内联视图.
我试过了:
connection = db.session.connection()
connection.execute( <sql here> )
Run Code Online (Sandbox Code Playgroud)
但我不断收到网关错误.
Mig*_*uel 283
你有没有尝试过:
result = db.engine.execute("<sql here>")
Run Code Online (Sandbox Code Playgroud)
要么:
from sqlalchemy import text
sql = text('select name from penguins')
result = db.engine.execute(sql)
names = [row[0] for row in result]
print names
Run Code Online (Sandbox Code Playgroud)
jpm*_*c26 150
如果您想使用会话(如您的问题所示),请execute
直接使用其方法:
result = db.session.execute('SELECT * FROM my_table WHERE my_column = :val', {'val': 5})
Run Code Online (Sandbox Code Playgroud)
以下可能特定于我的数据库驱动程序(psycopg2); 我不确定.无论如何,这是我如何取出我的价值观.
for r in result:
print(r[0]) # Access by positional index
print(r['my_column']) # Access by column name as a string
r_dict = dict(r.items()) # convert to dict keyed by column names
Run Code Online (Sandbox Code Playgroud)
关键是db.session
电话.这execute
部分只是我发现通过提供基于名称的访问使我的生活更轻松的东西.
此外,这是事务性的,无需手动管理.Say :val
是一个创建会话的函数:
from collections import namedtuple
Record = namedtuple('Record', result.keys())
records = [Record(*r) for r in result.fetchall()]
for r in records:
print(r.my_column)
print(r)
Run Code Online (Sandbox Code Playgroud)
Jak*_*ger 56
docs:SQL表达式语言教程 - 使用文本
例:
from sqlalchemy.sql import text
connection = engine.connect()
# recommended
cmd = 'select * from Employees where EmployeeGroup == :group'
employeeGroup = 'Staff'
employees = connection.execute(text(cmd), group = employeeGroup)
# or - wee more difficult to interpret the command
employeeGroup = 'Staff'
employees = connection.execute(
text('select * from Employees where EmployeeGroup == :group'),
group = employeeGroup)
# or - notice the requirement to quote "Staff"
employees = connection.execute(
text('select * from Employees where EmployeeGroup == "Staff"'))
for employee in employees: logger.debug(employee)
# output
(0, u'Tim', u'Gurra', u'Staff', u'991-509-9284')
(1, u'Jim', u'Carey', u'Staff', u'832-252-1910')
(2, u'Lee', u'Asher', u'Staff', u'897-747-1564')
(3, u'Ben', u'Hayes', u'Staff', u'584-255-2631')
Run Code Online (Sandbox Code Playgroud)
Dem*_*tri 53
从 SQLAlchemy 1.4 开始,无连接或隐式执行已被弃用,即
\ndb.engine.execute(...) # DEPRECATED\n
Run Code Online (Sandbox Code Playgroud)\n以及作为查询的裸字符串。
\n新的 API 需要显式连接,例如
\nfrom sqlalchemy import text\n\nwith db.engine.connect() as connection:\n result = connection.execute(text("SELECT * FROM ..."))\n for row in result:\n # ...\n
Run Code Online (Sandbox Code Playgroud)\n同样,it\xe2\x80\x99s 鼓励使用现有会话(如果可用):
\nresult = session.execute(sqlalchemy.text("SELECT * FROM ..."))\n
Run Code Online (Sandbox Code Playgroud)\n或使用参数:
\nsession.execute(sqlalchemy.text("SELECT * FROM a_table WHERE a_column = :val"),\n {\'val\': 5})\n
Run Code Online (Sandbox Code Playgroud)\n有关更多详细信息,请参阅文档中的“无连接执行、隐式执行”。
\nTri*_*ima 34
你可以使用SELECT SQL查询的结果from_statement()
,并text()
如图所示这里.你不必以这种方式处理元组.作为类名为'users'的用户的示例,您可以尝试,
from sqlalchemy.sql import text
.
.
.
user = session.query(User).from_statement(
text("SELECT * FROM users where name=:name")).\
params(name='ed').all()
return user
Run Code Online (Sandbox Code Playgroud)
Dev*_*evi 14
result = db.engine.execute(text("<sql here>"))
Run Code Online (Sandbox Code Playgroud)
<sql here>
除非你处于autocommit
模式,否则执行但不提交它.因此,插入和更新不会反映在数据库中.
要在更改后提交,请执行
result = db.engine.execute(text("<sql here>").execution_options(autocommit=True))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
221520 次 |
最近记录: |