微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在SqLite中的一个查询中更新和选择一行?

我想在sqlite中的一个查询中更新并选择一行.在MySql中我想要的查询看起来像这样:

SET @update_id := -1;
UPDATE data SET `Status` = 'running', Id = (SELECT @update_id := Id) 
  WHERE `Status` = 'scheduled' LIMIT 1;
SELECT * FROM data WHERE id=@update_id;"

上面的查询将Status设置为’running’,将变量@update_id的值设置为具有Status’状态’的第一行的修改行的Id,而不是使用变量@update_id来获取完整的修改行.

重要的是我需要选择UPDATE语句修改过的行

但据我所知,sqlite不支持变量.

如何为sqlite重写上面的MySQL查询

解决方法:

你需要declare and use variables in whatever program you write that runs SQLite statements.

谢天谢地,您可以使用bind variables in SQLite

//1.  Fetch the id into whatever language you are using:
SELECT id FROM DATA WHERE status = 'scheduled' LIMIT 1;

//2. Retrieve the id value from Step 1's resultset
int id = [id value from the resultset];

//3. Construct the update statement
parsed_sql_stmt stmt = parse_sql(UPDATE DATA SET STATUS = 'running' WHERE ID = ?);

//4. Execute update statement
exec_stmt(stmt, id);

//5. Select everything in the DATA table for that record
stmt = parse_sql(SELECT * FROM DATA WHERE id = ?);
exec_stmt(stmt, id);

sheepsimulator是对的 – 这是三个单独的陈述.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐