我想在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是对的 – 这是三个单独的陈述.
原文地址:https://codeday.me/bug/20190622/1259487.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。