QSqlQuery query("SELECT * FROM my_table"); if (query.exec()) { int num_rows = query.size(); qDebug() << "Number of rows retrieved: " << num_rows; } else { qDebug() << "Query failed!"; }
QSqlQuery query; query.prepare("INSERT INTO my_table (name) VALUES (:name)"); query.bindValue(":name", "John Doe"); if (query.exec()) { qDebug() << "Inserted row ID: " << query.lastInsertId(); } else { qDebug() << "Insert failed!"; }In this example, we create a query object and prepare an INSERT statement with a named parameter. We use the `bindValue()` method to set the parameter value, then execute the query. If the query is successful, we use the `lastInsertId()` method to retrieve the ID of the newly inserted row. The QSqlQuery class is part of the Qt SQL module/library/package.