admin管理员组文章数量:1296454
I have the following:
def execute_query(sql_query, engine, params_tuple=None):
params_tuple = params_tuple if params_tuple else {}
with engine.connect() as connection:
result = connection.execute(text(sql_query), params_tuple)
connectionmit()
This method seems to run sucessfully when params_tuple is None
, but it fails otherwise. Here is an example:
INSERT INTO x.aggregation_dates (position_date, account_id, horizon)
VALUES (?, ?, ?)
where params_tuple = ('2025-01-31', 12, 22)
Why is this not working? What am I missing?
The error I get is:
List argument must consist only of tuples or dictionaries
Thank you!
I have the following:
def execute_query(sql_query, engine, params_tuple=None):
params_tuple = params_tuple if params_tuple else {}
with engine.connect() as connection:
result = connection.execute(text(sql_query), params_tuple)
connectionmit()
This method seems to run sucessfully when params_tuple is None
, but it fails otherwise. Here is an example:
INSERT INTO x.aggregation_dates (position_date, account_id, horizon)
VALUES (?, ?, ?)
where params_tuple = ('2025-01-31', 12, 22)
Why is this not working? What am I missing?
The error I get is:
List argument must consist only of tuples or dictionaries
Thank you!
Share Improve this question edited Feb 12 at 4:04 Parfait 108k19 gold badges100 silver badges134 bronze badges asked Feb 11 at 22:47 DDigitsDDigits 1577 bronze badges 1- 1 Related (though I wouldn't necessarily recommend it). – snakecharmerb Commented Feb 12 at 6:57
1 Answer
Reset to default 1Passing a tuple directly to the parameters via sqlalchemy will not work. You have two options:
Pass the params individually
Change the tuple to a dictionary, naming each column. And use this in the params.
I'd recommend this approach, though it involves more changes than the approach below it is closer to how sqlalchemy intends you to do this. See their guide here for an example: https://docs.sqlalchemy./en/14/core/tutorial.html#using-textual-sql
import asyncio
from sqlalchemy import text, tuple_
from . import DB
async def main():
async with DB() as session:
params_dict = {"position_data": "2025-01-31", "account_id": 12, "horizon": 22}
sql_query = f"""
INSERT INTO aggregation_dates VALUES (:position_data, :account_id, :horizon);
"""
await session.execute(text(sql_query), params_dict)
await sessionmit()
asyncio.run(main())
Use string formatting
If you substitute the tuple into the raw sql directly, the insert command will work. This is less of a good idea because directly formatting like this does not sanitize the input, so I wouldn't recommend this approach. Although it is quick and easy, only do this if this is just personal code imo.
import asyncio
from sqlalchemy import text, tuple_
from . import DB
async def main():
async with DB() as session:
params_tuple = ("2025-01-31", 12, 22)
sql_query = f"""
INSERT INTO aggregation_dates VALUES {params_tuple};
"""
await session.execute(text(sql_query))
await sessionmit()
asyncio.run(main())
本文标签: pythonIssue with parameters of SQLAlchemy execute()Stack Overflow
版权声明:本文标题:python - Issue with parameters of SQLAlchemy execute() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741630112a2389311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论