QSqlQuery query; query.exec("SELECT * FROM table"); if (query.lastError().isValid()) { qDebug() << "Error: " << query.lastError().text(); }
QSqlQuery query; query.prepare("INSERT INTO table (name, age) VALUES (?, ?)"); query.bindValue(0, "John"); query.bindValue(1, 30); if (!query.exec()) { qDebug() << "Error: " << query.lastError().text(); }In this example, we prepare an INSERT query with placeholders and bind values to them using the bindValue method. We then execute the query and check for errors using the lastError method. The QSqlQuery class is part of the QtSql module in the Qt framework.