QSqlQuery query; query.prepare("INSERT INTO students (name, age) VALUES (:name, :age)"); query.bindValue(":name", "John Doe"); query.bindValue(":age", 25); if (query.exec()) { int rowsAffected = query.numRowsAffected(); qDebug() << rowsAffected << " rows affected."; } else { qDebug() << "Error executing query: " << query.lastError().text(); }
QSqlQuery query; query.prepare("UPDATE students SET age = 26 WHERE name = 'John Doe'"); if (query.exec()) { int rowsAffected = query.numRowsAffected(); qDebug() << rowsAffected << " rows affected."; } else { qDebug() << "Error executing query: " << query.lastError().text(); }In this example, we create a SQL query to update the age of a student named "John Doe" in the "students" table. If the query is executed successfully, we get the number of rows affected by calling the numRowsAffected method, and print it to the console. Package/library: Qt library.