为了账号安全,请及时绑定邮箱和手机立即绑定

SQLAlchemy TypeError:(“参数必须位于列表、元组或行中”,“HY000”)

SQLAlchemy TypeError:(“参数必须位于列表、元组或行中”,“HY000”)

汪汪一只猫 2023-12-12 10:20:08
我有一个使用 sqlalchemy 的方法    def insert_amended_values(self, data):        insert_stmt = """INSERT INTO amended (date, volume, price, updated)        VALUES (%(date)s, %(volume)s, %(price)s, %(updated)s);"""        crsr = self.connection.engine.raw_connection().cursor()        crsr.executemany(insert_stmt, data)其中需要的数据是字典列表,例如[    {'date': '2020-06-27', 'volume': '30', 'price': 50, 'updated': '2020-10-21 17:17:50'},    {'date': '2020-06-28', 'volume': '32', 'price': 48, 'updated': '2020-10-21 17:17:50'},    {'date': '2020-06-29', 'volume': '26', 'price': 56, 'updated': '2020-10-21 17:17:50'}]但我得到了错误TypeError: ('Params must be in a list, tuple, or Row', 'HY000')如何将字典列表转换为列表或行列表,同时保留参数化查询?
查看完整描述

1 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

将数据保留在listof中dict,将 SQL 语句包装在 SQLAlchemytext对象中并使用:name参数样式


import sqlalchemy as sa


# …


    def insert_amended_values(self, data):

        insert_stmt = sa.text("""INSERT INTO amended (date, volume, price, updated)

        VALUES (:date, :volume, :price, :updated);""")

        with self.connection.engine.begin() as conn:

            conn.execute(insert_stmt, data)


查看完整回答
反对 回复 2023-12-12
  • 1 回答
  • 0 关注
  • 40 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信