0

I have a graphical interface which can display multiple SQLite query results in their own tabs. Now, I am finalizing a sqlite3_stmt statement when the user closes its tab, not immediately. If there is more than one tab/result, the first one gives me exactly one sqlite3_stmt, but the second one gives me two: the sqlite3_stmt from the second tab, plus the one from the first tab. A third tab would give me 3 results, and so on.

Simplified Delphi code:

var
    FHandle: sqlite3;
    QueryResult: sqlite3_stmt;
begin
    QueryResult := nil;
    QueryStatus := sqlite3_prepare_v2(FHandle, SQL, -1, QueryResult, nil);
    if QueryStatus = SQLITE_OK then begin
        QueryResult := sqlite3_next_stmt(FHandle, nil);
        while QueryResult <> nil do begin
            while sqlite3_step(QueryResult) = SQLITE_ROW do begin
              // store row data...
            end;
            // points to a previously prepared query:
            QueryResult := sqlite3_next_stmt(FHandle, QueryResult);
        end;
    end;
end;

How do I only return the statement(s) from the just fired SQL, not from an earlier one?

Edit: I just tried sqlite3_prepare_v3, which has a prepFlags parameter where I can pass SQLITE_PREPARE_PERSISTENT. But that did not seem to change anything here.

Anse
  • 1,573
  • 12
  • 27

1 Answers1

0

I think I overread the pzTail parameter in the sqlite3_prepare_* functions.

So, instead of iterating over sqlite3_next_stmt(), I am now calling sqlite3_prepare_v2 with the pzTail string from the last loop:

while sqlite3_prepare_v2(FHandle, CurrentSQL, -1, QueryResult, NextSQL) = SQLITE_OK do begin
    // store QueryResult for later use...
    while sqlite3_step(QueryResult) = SQLITE_ROW do begin
      // store row data for later use...
    end;
    CurrentSQL := NextSQL;
    if Trim(CurrentSQL) = '' then
      break;
end;

This thread on nabble.com hit me into the right direction here.

Anse
  • 1,573
  • 12
  • 27