1

I am using a python to update entries in Sqlite table. The command I am using is:

handle.execute("UPDATE RECORD set NAME=%s DEVICE=%s PROJECT=%s IP=%s COMMENT=%s where ID = %s"%(arg[2],arg[3],arg[4],arg[5],arg[6],arg[1]))

To this I get am getting an error as:

sqlite3.OperationalError: near "DEVICE": syntax error

I cannot understand what is specifically wrong with Device. Also I have checked the variables are as expected. The data base has a column named device and the database can be opened / accessed and edited using this python file.

1 Answer 1

1

There are commas missing between set items.

In addition to that, instead of string formatting, pass parameters to prevent SQL injection:

handle.execute(
    """UPDATE RECORD
    SET NAME=%s, DEVICE=%s, PROJECT=%s, IP=%s, COMMENT=%s
    WHERE ID = %s""",
    (arg[2], arg[3], arg[4], arg[5], arg[6], arg[1]))

UPDATE If you insist to use string formatting, you should quote %s: '%s'

Sign up to request clarification or add additional context in comments.

3 Comments

Adding commas between columns worked. Now it is giving me an error as "sqlite3.OperationalError: no such column: john". Here John is the variable I am trying to update in column:- 'name'. Instead why is it searching for a column named john ?
@john, You need to quote values %s -> '%s'. BTW , as I mentioned in the answer, you'd better to use parameter passing instead of string formatting yourself.
SET NAME='%s', DEVICE='%s', PROJECT='%s', IP='%s', COMMENT='%s' WHERE ID = '%s'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.