亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號