QSqlQuery query; query.exec("SELECT COUNT(*) FROM customers"); if (query.next()) { int count = query.value(0).toInt(); qDebug() << "Number of customers: " << count; }
QSqlQuery query; query.exec("SELECT name, age FROM customers"); while (query.next()) { QString name = query.value(0).toString(); int age = query.value(1).toInt(); qDebug() << "Name: " << name << ", Age: " << age; }In this example, we execute a query that returns two columns: the name and age of all customers in the database. We then fetch each value using the `value()` method of QSqlQuery and convert them to a string and integer, respectively. We use a `while` loop to iterate through all the rows returned by the query. The QSqlQuery class is part of the QtSql module in the Qt library.