Reputation: 47206
Is it possible to re-use the sqlite3 output?I have query like this :
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.");
and results are parsed
while (sqlite3_step(res) == SQLITE_ROW)
//do something here with results for device X
Now I would like to re-use the output 'res' for example, the code flow will be like
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
How to achieve this ? I prefer not to re-run the same sql statement for the second time and fetch the results.
Upvotes: 1
Views: 752
Reputation: 41180
You must either cache the data yourself, or re-run the query.
Read about the reasons on this page about Sqlite and Scrolling Cursors, including:
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.
Upvotes: 2