Home > Mobile >  sqlite3: conditional Insert in table using python
sqlite3: conditional Insert in table using python

Time:10-29

I am trying to insert a value using following code

insert_query = """INSERT OR REPLACE INTO results
                          (output_text, processed) 
                           VALUES 
                          ('html', 1)
                          select  user_id, user_token FROM results
                          WHERE
                          user_id = usr_id AND user_token = usr_token"""

cur.execute(insert_query)

I am getting

OperationalError: near "select": syntax error

WHERE can't be used with INSERT that's why I am using SELECT but still I am getting this error. Can someone help wit this issue? Thank you

CodePudding user response:

I suggest you having a look at the sqlite syntax for insertion.

To me it seems that for what you are trying to achieve, an UPDATE statement would be more appropriate

CodePudding user response:

You should remove the VALUES ('html', 1) if you have a SELECT. You can't have VALUES and a SELECT in the same INSERT. See sqlite documentation for more information.

This should work:

insert_query = """INSERT OR REPLACE INTO results
                          (output_text, processed) 
                          select  user_id, user_token FROM results
                          WHERE
                          user_id = usr_id AND user_token = usr_token"""
  • Related