Search code examples
sqliteblackberry-10blackberry-cascadesqtsql

Query to return Count using QtSql


How can I execute the following query using the QtSql library?

const QString createQuery = "SELECT COUNT(*) FROM movies;";

I tried the following code, but that returns a bool.

QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName("/path/to/database");
QSqlQuery query(database);

query.exec(createQuery);//returns bool

Solution

  • The exec function does execute the query; that's what it return value says.

    To read the returned value, navigate to the first returned row, then get the value from the first column:

    query.first();
    count = query.value(0).toInt();
    

    The documentation might be helpful.