QSqlQuery query; query.prepare("SELECT name, age FROM people WHERE age > ?"); query.addBindValue(20); if (query.exec()) { while (query.next()) { QString name = query.value("name").toString(); int age = query.value("age").toInt(); // process data ... } } else { qWarning() << "Query failed:" << query.lastError().text(); }
QSqlQuery query; query.prepare("INSERT INTO people (name, age, city) VALUES (:name, :age, :city)"); query.bindValue(":name", "John"); query.bindValue(":age", 30); query.bindValue(":city", "New York"); if (query.exec()) { // query successful } else { qWarning() << "Query failed:" << query.lastError().text(); }
QSqlQuery query; query.prepare("UPDATE people SET age = ? WHERE name = ?"); query.addBindValue(40); query.addBindValue("John"); if (query.exec()) { // query successful } else { qWarning() << "Query failed:" << query.lastError().text(); }In summary, the QSqlQuery class in C++ is part of the Qt SQL module, which provides a high-level interface for executing SQL queries on a database. The examples above demonstrate how to use QSqlQuery to perform SELECT, INSERT, and UPDATE queries using different parameter binding techniques.