[Qt]recuperer les données d'un SELECT

kamy86
[Qt]recuperer les données d'un SELECT

salut,
je veux faire un select dans ma base de données mais je sait pas comment faire pour recuperer les données et les afficher sur ecran.
SVP comment je fais? :roll:

arnaud_dupuis

A mon avis un petit tour par la documentation du module QtSql pourrait être utile :
http://doc.trolltech.com/4.4/qtsql.html

Morceau choisi :

Quote:

Executing SQL Statements

The QSqlQuery class provides an interface for executing SQL statements and navigating through the result set of a query.

The QSqlQueryModel and QSqlTableModel classes described in the next section provide a higher-level interface for accessing databases. If you are unfamiliar with SQL, you might want to skip directly to the next section (Using the SQL Model Classes).
Executing a Query

To execute an SQL statement, simply create a QSqlQuery object and call QSqlQuery::exec() like this:

     QSqlQuery query;
     query.exec("SELECT name, salary FROM employee WHERE salary > 50000");

The QSqlQuery constructor accepts an optional QSqlDatabase object that specifies which database connection to use. In the example above, we don't specify any connection, so the default connection is used.

If an error occurs, exec() returns false. The error is then available as QSqlQuery::lastError().
Navigating the Result Set

QSqlQuery provides access to the result set one record at a time. After the call to exec(), QSqlQuery's internal pointer is located one position before the first record. We must call QSqlQuery::next() once to advance to the first record, then next() again repeatedly to access the other records, until it returns false. Here's a typical loop that iterates over all the records in order:

     while (query.next()) {
         QString name = query.value(0).toString();
         int salary = query.value(1).toInt();
         qDebug() << name << salary;
     }

The QSqlQuery::value() function returns the value of a field in the current record. Fields are specified as zero-based indexes. QSqlQuery::value() returns a QVariant, a type that can hold various C++ and core Qt data types such as int, QString, and QByteArray. The different database types are automatically mapped into the closest Qt equivalent. In the code snippet, we call QVariant::toString() and QVariant::toInt() to convert variants to QString and int.

You can iterate back and forth using QSqlQuery::next(), QSqlQuery::previous(), QSqlQuery::first(), QSqlQuery::last(), and QSqlQuery::seek(). The current row index is returned by QSqlQuery::at(), and the total number of rows in the result set is avaliable as QSqlQuery::size() for databases that support it.

To determine whether a database driver supports a given feature, use QSqlDriver::hasFeature(). In the following example, we call QSqlQuery::size() to determine the size of a result set of the underlying database supports that feature; otherwise, we navigate to the last record and use the query's position to tell us how many records there are.

     QSqlQuery query;
     int numRows;
     query.exec("SELECT name, salary FROM employee WHERE salary > 50000");

     QSqlDatabase defaultDB = QSqlDatabase::database();
     if (defaultDB.driver()->hasFeature(QSqlDriver::QuerySize)) {
         numRows = query.size();
     } else {
         // this can be very slow
         query.last();
         numRows = query.at() + 1;
     }

If you iterate through a result set only using next() and seek() with positive values, you can call QSqlQuery::setForwardOnly(true) before calling exec(). This is an easy optimization that will speed up the query significantly when operating on large result sets.

La page d'origine ayant un bien meilleur formatage, je vous conseil de préférer celle ci :).