不要在表格中保存信息



我试图通过以下方式将信息保存到数据库:

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)

乌利希期刊指南:检查数据和类型,获取下一个

'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()

但是我想更改表

问题解决,运行正常:

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()

最新更新