[英]SQLite3 Python variables
我在 python 中為 SQLite3 數據庫編寫了以下代碼:
i = datetime.datetime.now()
v_day = i.day
v_month = i.month
v_year = i.year
v_hour = i.hour
v_minute = i.minute
cur.execute("""INSERT INTO weather(
day,month,year,hour,minut,temperature,humidity) VALUES (
?,?,?,?,?,?,?),
(v_day,v_month,v_year,v_hour,v_minute,temp,hum)""")
嘗試后,它顯示此錯誤:文件“store_data.py”,第 68 行,在 main (v_day,v_month,v_year,v_hour,v_minute,temp,hum)""") sqlite3.OperationalError: no such column: v_day I '已經嘗試將變量名稱放入 VALUES' 列表中,但我出現了相同的錯誤。
謝謝你的回答。
你的元組在你的字符串里面。 此外,您不應該在查詢中表示表列。 嘗試
cur.execute("INSERT INTO weather VALUES (?,?,?,?,?,?,?)",
(v_day,v_month,v_year,v_hour,v_minute,temp,hum))
試試這個:
sql = f"""INSERT INTO weather(day,month,year,hour,minut,temperature,humidity) VALUES ({v_day},{v_month},{v_year},{v_hour},{v_minute},{temp},{hum})"""
cur.execute(sql)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.