我試圖通過以下方式將資訊保存到資料庫中:
def insert_into_db_before_eat(table, id_row, time, sugar, actra_ui, xe):
cur.execute(f"INSERT INTO {table} VALUES ({id_row},{time},{sugar},{actra_ui},{xe})")
con.commit()
但我收到以下錯誤:
cur.execute(f"INSERT INTO {table} VALUES ({int(id_row)},{time},{sugar},{actra_ui},{xe})")
sqlite3.OperationalError: near "17": syntax error
使用下一個資料:
('Sugar_before_eat_after_sleep', 17, 2021-10-24 17:01:21, 1, 10, 5)
UPD:檢查資料和型別,下一步
'Sugar_before_eat_after_sleep', 17, 2021-10-24 17:39:33, 10, 5, 2
<class 'str'>, <class 'int'>, <class 'datetime.datetime'>, <class 'str'>, <class 'str'>, <class 'str'>
UPD2:如果我使用下一個代碼,它的作業:
def insert_into_db_before_eat(id_row, time, sugar, actra_ui, xe):
cur.execute("INSERT INTO Sugar_before_eat_after_sleep VALUES (?, ?, ?, ?, ?)", (id_row, time, sugar, actra_ui, xe))
con.commit()
但我想換表
uj5u.com熱心網友回復:
問題已解決,有效:
insert_into_db_before_eatt(id_row, time, sugar, actra_ui, xe):
table = 'Sugar_before_eat_after_sleep'
cur.execute("INSERT INTO %s VALUES (?, ?, ?, ?, ?)"% table,
(id_row, time, sugar, actra_ui, xe))
con.commit()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/340591.html
