当前位置:首页 > 数据库 > SQlite

如何在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是对的 – 这是三个单独的陈述.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!