Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sqlite3.OperationalError: near "?": syntax error

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading