0

This is trivial in mysql thanks to mysql_num_rows but no such equivalent is present in sqlite3. Hence the question is how to know if the current row is the last row.

Rearranging like following doesn't help as any previous binding after sqlite3_step is not valid.

sqlite3_prepare_v2()
int fetched = 0;
int last = 0;
while (sqlite3_step(statement) == SQLITE_ROW) {
       // this is the previous row
       if(fetched) {
           process(data, last);
       }
       fetched = 1;
       data = sqlite3_column_text();
}
last = 1;
if(fetched)
    process(data, last);

Executing query twice (one with count) is a trivial solution but that's not what I am looking for.

Any ideas? thanks in advance.

mesibo
  • 3,970
  • 6
  • 25
  • 43
  • If you did not create the table using the `WITHOUT ROWID` clause, `SELECT Max(ROWID) AS LastRow` should give you the last created row. `AS LastRow` is optional. – Phantômaxx Sep 24 '16 at 09:03

1 Answers1

0

SQLite computes the next output row only when needed. So it is not possible to find out if you can get another row without actually trying to step to that row.

If your code really needs to know whether the current row is the last, you have to make a copy of all the data in the row.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • Thanks, that's what we are doing right now but not efficient and hence was checking if there was a way which we overlooked. – mesibo Sep 23 '16 at 18:05