Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Selecting data from a database without using SQL statements in Qt

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic, Nokia N900
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QSqlTableModel
Created: tepaa (09 Sep 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Overview

This example shows how to select data with QSqlTableModel from an SQLite database in Qt without using any SQL statements.

The table 'person' has the following columns:

  • id (integer primary key), this is an auto-increment field
  • firstname (varchar(20))
  • lastname (varchar(30))
  • age (integer)


Preconditions

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

#include <QSqlTableModel>
#include <QSqlRecord>
#include <QString>

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

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

See also

This page was last modified on 11 October 2012, at 01:18.
291 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×