sqlsqliteqtqsqldatabase

QSqlDatabase lastError() returns empty string for error in query


I was writing simple code using SQLite database in Qt:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QSqlQuery query;
query.exec("CREATE TABLE IF NOT EXISTS test(id, name)");
// ... adding data here
if(query.exec("SELECT * from test")) {
    while(query.next()) {
        qDebug() << query.value(0).toString();
    }
} else {
    qDebug() << "Error: " << db.lastError().text();
}

and noticed, that I get empty string instead of error text, if I make some mistake in query (for example, misspell table name - "SELECT * from tesst"). So, why is that?


Solution

  • Turned out, for catching query errors, I should be using query.lastError().text() instead of db.lastError().text()