gpt4 book ai didi

python - 如何将 python `dict` 转换为 `sqlalchemy.engine.row`

转载 作者:行者123 更新时间:2023-12-05 06:00:07 26 4
gpt4 key购买 nike

我知道我们可以通过 row_dict = dict(row)sqlalchemy.engine.row 转换为 dict。是否有可能做相反的事情,即像 row = Row(row_dict) 这样的事情?我试过了,但没有用,错误为 *** TypeError: BaseRow expected 5 arguments, got 1

最佳答案

至少你可以做到

import sqlalchemy as sa

engine = sa.create_engine("sqlite:///:memory:")

# test data
data = {"col1": "foo", "col2": "bar"}

sql = "SELECT " + ", ".join([f":{k} as {k}" for k in data])
print(sql) # SELECT :col1 as col1, :col2 as col2

with engine.begin() as conn:
row = conn.execute(sa.text(sql), data).fetchone()

print(type(row)) # <class 'sqlalchemy.engine.row.LegacyRow'>
print(row) # ('foo', 'bar')
print(row.col1) # foo

关于python - 如何将 python `dict` 转换为 `sqlalchemy.engine.row`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67841745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com