QSqlQuery query("SELECT * FROM mytable"); if (query.isSelect()) { // Handle the result set while (query.next()) { int id = query.value("id").toInt(); QString name = query.value("name").toString(); qDebug() << "ID: " << id << " Name: " << name; } } else { // Handle other types of queries }
QSqlQuery query; query.prepare("INSERT INTO mytable (name) VALUES (?)"); query.bindValue(0, "John"); if (query.exec()) { qDebug() << "Record inserted successfully"; } else { qDebug() << "Error executing query: " << query.lastError().text(); }In this example, we prepare and execute an INSERT statement to add a new record to the mytable table. We check the return value of the exec() function to determine whether the query was executed successfully. The QSqlQuery class is part of the QtSql package, which is a library for working with SQL databases in Qt.