Selecting data from a database without using SQL statements in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Overview[edit | edit source]

Template:Abstract

The table 'person' has the following columns:

  • 76ytuiytuityutyutututyutyutyu, this is an auto-increment field
  • 76ytuiytuityutyutututyutyutyu
  • 76ytuiytuityutyutututyutyutyu
  • 76ytuiytuityutyutututyutyutyu


Preconditions[edit | edit source]

Download and install the Qt SDK

For Maemo SQLite development, the following packages must be installed:

  • libqt4-sql
  • libqt4-sql-sqlite
  • libsqlite3-0
  • libsqlite3-dev

Header[edit | edit source]

  1. include <QSqlTableModel>
  2. include <QSqlRecord>
  3. include <QString>

Source[edit | edit source]

Select person data. The person ID is given as a filter for the selection.

QString MyWidget::searchPersonName(int personId)

   {
   QString name;    
   QSqlTableModel *model = new QSqlTableModel;
   // Set used table
   model->setTable("person");
   model->setEditStrategy(QSqlTableModel::OnManualSubmit);
   // Set where clause
   model->setFilter(QString("id=%1").arg(personId));
   model->select();
   //The index of the record we are interested in
   int index = 0; 
   // Read result
   QSqlRecord record = model->record(index);
   if (!record.isEmpty())
       name = record.value(2).toString();
   return name;
   }

Postconditions[edit | edit source]

The person's data is selected from the database without using SQL statements.

See also[edit | edit source]

pt:Como selecionar informações do banco de dados sem o uso de declaração SQL em Qt