gpt4 book ai didi

c - sqlite3_step : how to parse the output more than once ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:25:27 24 4
gpt4 key购买 nike

是否可以重新使用 sqlite3 输出?我有这样的查询:

            error = sqlite3_prepare_v2(conn,SQL_STMT_GET_FILE,-1, &res, &tail);
error = sqlite3_bind_text(res, 1,file,strlen(file), SQLITE_STATIC); assert(error == SQLITE_OK);
if (error != SQLITE_OK) {
handle_error("No matching record found.");

解析结果

    while (sqlite3_step(res) == SQLITE_ROW) 
//do something here with results for device X

例如,现在我想重新使用输出“res”,代码流程如下

    while (sqlite3_step(res) == SQLITE_ROW) 
//do something here with results for device X


//reset the cursor to starting position and scan through the records.

while (sqlite3_step(res) == SQLITE_ROW)
//do something here with results for device Y

如何实现?我不想第二次重新运行相同的 sql 语句并获取结果。

最佳答案

您必须自己缓存数据,或者重新运行查询。

了解原因 on this page about Sqlite and Scrolling Cursors,包括:

The problem is that the sqlite3_step() function does not step through a precomputed result set at all. A better and more realistic way to think about matters is to suppose that each prepared statement is really a computer program. You are running this program in a debugger and there is a breakpoint set on a single statement somewhere deep down inside the computation. Calling the sqlite3_step() function is like pressing the "Run" button in your debugger and thus asking the debugger to run the program until it either exits or hits the breakpoint. Sqlite3_step() returns SQLITE_ROW if it hits the breakpoint and SQLITE_DONE if it finishes. If you hit the breakpoint, you can then look at local variable in order to find the values of a "row". Then you can press the "Run" button (call sqlite3_step()) again to continue execution until the next breakpoint or until the program exits.

关于c - sqlite3_step : how to parse the output more than once ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com