简体   繁体   English

sqlite3:使用python在表中条件插入

[英]sqlite3: conditional Insert in table using python

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) cur.execute(insert_query)

I am getting我正进入(状态

OperationalError: near "select": syntax error OperationalError:接近“选择”:语法错误

WHERE can't be used with INSERT that's why I am using SELECT but still I am getting this error. WHERE不能与INSERT一起使用,这就是我使用SELECT但仍然收到此错误的原因。 Can someone help wit this issue?有人可以帮助解决这个问题吗? Thank you谢谢

I suggest you having a look at the sqlite syntax for insertion .我建议您查看用于插入sqlite 语法

To me it seems that for what you are trying to achieve, an UPDATE statement would be more appropriate对我来说,对于您想要实现的目标, UPDATE 语句似乎更合适

You should remove the VALUES ('html', 1) if you have a SELECT .如果您有SELECT VALUES ('html', 1)您应该删除VALUES ('html', 1) You can't have VALUES and a SELECT in the same INSERT .在同一个INSERT不能有VALUESSELECT See sqlite documentation for more information.有关更多信息,请参阅sqlite 文档

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"""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM