Skip to content
Advertisement

Don’t save info in table

I am trying to save the information to the database in the following way:

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

But I get the following error:

cur.execute(f"INSERT INTO {table} VALUES ({int(id_row)},{time},{sugar},{actra_ui},{xe})")
sqlite3.OperationalError: near "17": syntax error

Using next data:

('Sugar_before_eat_after_sleep', 17, 2021-10-24 17:01:21, 1, 10, 5)

UPD: Check for data and types, get next

'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: If I use next code, it’s work:

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

But I want to change table

Advertisement

Answer

Issue resolved, it works:

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()
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement