QSqlQuery query; query.exec("SELECT * FROM non_existing_table"); if (query.lastError().isValid()) { qDebug() << "Error message: " << query.lastError().text(); }
QSqlQuery query; query.exec("INSERT INTO table (column) VALUES ('value')"); if (query.lastError().isValid()) { qDebug() << "Error code: " << query.lastError().nativeErrorCode(); }In this example, we create a QSqlQuery object and execute an INSERT statement on a table. We then check if an error occurred by calling the isValid method on the lastError object of the query. If it returns true, we print the driver specific error code using the nativeErrorCode method of this object. Package library: QtSql.