Results from two queries at once in sqlite? Results from two queries at once in sqlite? sqlite sqlite

Results from two queries at once in sqlite?


You can make the count query return the same number of columns as the selection query and make a UNION of the count query and the selection one.

The first row of the result set will contain the total count then.

Another possible solution is described in the post about SQL_CALC_FOUND_ROWS from sqlite-users maillist.

And a small notice about your selection query: if you insert records into the Log table with datetime('now'), and id is the auto incremented primary key of the table, then sorting by time is not required and it's enough to sort by id DESC. Since the auto incremented primary key field is an alias to ROWID, you will get a significant performance improvement.

By the way, time is a built in functions in SQLLite, so you should quote the column name with back-ticks (`).


You can use your query with the sqlite3_get_table function to collect the rows
and get the number of result rows.


You can use triggers to keep a count of the number of entries at each level in a separate table. The triggers need to increment the count when an entry is inserted, and decrement the count when an entry is deleted.

Example:

create table Log_counts (level primary key, count);create trigger Log_insert_trigger after insert on Logs  for each row  begin    update Log_counts set count = count + 1 where level = new.level;  end;create trigger Log_delete_trigger after delete on Logs  for each row  begin    update Log_counts set count = count - 1 where level = old.level;  end;

You will need to initialize Log_counts with the counts per level; in an empty database, for each level...

insert into Log_counts (level, count) values (%s, 0);

Then you will not need a count(*) query for every page of display.