iwongu / sqlite3pp

SQLite3++ - C++ wrapper of SQLite3 API
MIT License
609 stars 177 forks source link

Row count of table? #71

Closed OOHehir closed 1 year ago

OOHehir commented 2 years ago

Hello, I've taken a look at this & can't seem to find a decent answer.

Is there any suggested method to get the row count (i.e. length) of a table? I've tried using COUNT(*) without success & also something trivial like:

int row_count = 0;
sqlite3pp::query qry(this->db, "SELECT * FROM some_table");
auto i = qry.begin();
if (i != qry.end())
    row_count++;

Thanks

iwongu commented 1 year ago

You can use COUNT(*). Check https://github.com/iwongu/sqlite3pp/blob/master/test/testdb.cpp#L50

sqlite3pp::query qry(db, "SELECT COUNT(*) FROM contacts");
auto iter = qry.begin();
int count = (*iter).get<int>(0);
expect_eq(3, count);