qt sqlQueyer.exec()
时间: 2024-05-01 21:17:15 浏览: 7
The `exec()` function is a method of the `QSqlQuery` class in Qt, which is used to execute an SQL query on a database.
The `exec()` function returns a boolean value that indicates whether the query was successful. It can be used to execute any SQL statement, such as SELECT, INSERT, UPDATE, or DELETE.
Here is an example of using `exec()` to execute a SELECT query:
```
QSqlQuery query;
query.exec("SELECT * FROM myTable");
while (query.next()) {
QString name = query.value("name").toString();
int age = query.value("age").toInt();
qDebug() << "Name:" << name << ", Age:" << age;
}
```
This code creates a `QSqlQuery` object, and then calls `exec()` with a SELECT statement to retrieve all records from a table named "myTable". The `next()` function is used to iterate through each row returned by the query, and the `value()` function is used to retrieve the values of the "name" and "age" columns for each row. Finally, the results are printed to the console using `qDebug()`.