[英]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
不能有VALUES
和SELECT
。 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.