我正在嘗試使用下面的代碼將 Python 字典串列轉換為 SQLite 表。
conn = sqlite3.connect('stockData.db')
c = conn.cursor()
c.executemany(f"INSERT INTO AAPL1M2021 (time,open,high,low,close,volume) VALUES (%(t)s,%(o)s,%(h)s,%(l)s,%(c)s,%(v)s)", data)
每當我運行此代碼時,它都會回傳以下錯誤。
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
<ipython-input-7-dce4eb1e6f6f> in <module>
2 conn = sqlite3.connect('stockData.db')
3 c = conn.cursor()
----> 4 c.executemany("INSERT INTO AAPL1M2021 (time,open,high,low,close,volume) VALUES (%(t)s,%(o)s,%(h)s,%(l)s,%(c)s,%(v)s)", data)
OperationalError: near "%": syntax error
我一定遺漏了一些東西,因為我似乎找不到解決方案。
這是字典串列中的示例字典。
{'v': 26666,
'vw': 133.233,
'o': 133.31,
'c': 133.49,
'h': 133.49,
'l': 133.02,
't': 1609750800000,
'n': 87}
uj5u.com熱心網友回復:
Python 的sqlite包不使用%s樣式格式。它?用于位置占位符或:name關鍵字占位符。
所以你的查詢可能看起來像
c.execute(
"INSERT INTO AAPL1M2021 (time,open,high,low,close,volume) VALUES (:t, :o, :h, :l, :c, :v)",
data
)
請注意,executemany用于執行多個查詢的引數串列。對于單個 dict 使用execute;使用executemany的型別的字典串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/401088.html
