Home > Software design >  sqlite3.OperationalError: near "?": syntax error
sqlite3.OperationalError: near "?": syntax error

Time:12-01

I'm making a database that stores specific times so i want to update all values in it but when I try to update it gives me that error sqlite3.OperationalError: near "?": syntax error I don't know why. This is my code:

def db_edit(db , cr , table , new_value , title):
    time = check_txt_len(new_value , 8 , "0")
    tu = (table , time , time[0:2] , time[3:5] , time[6:] , title)
    cr.execute("UPDATE ? SET title=? , hour=? , min=? , period=? where title=?" , tu)
    db.commit()
    data = db_get("*" , "times" , "fetchall" , cr)
    for tu in data:
        for item in tu:
            if item is title:
                return False
    return True

this is the error part:

cr.execute("UPDATE ? SET title='?' , hour='?' , min=? , period='?' where title='?'" , tu)

CodePudding user response:

Yes, ? cannot be used for table names or field names, because those don't get quoted. It is unusual that this function doesn't already know the table name. How could it know the fields, but not the table?

And it's silly to set the title, when you are doing an update based on the title.

What is that loop trying to do? It looks like you're trying to return False if the title is found in any record, but you KNOW it has to be there, because you just did an UPDATE WHERE title='title'. In any case, "is" is the wrong operator for that.

def db_edit(db , cr , table , new_value , title):
    time = check_txt_len(new_value , 8 , "0")
    tu = (time[0:2] , time[3:5] , time[6:] , title)
    cr.execute(f"UPDATE {table} SET hour=?, min=?, period=? WHERE title=?", tu)
    return True
  • Related