Namespaces
Variants
Actions

Selecting data from a database without using SQL statements in Qt

Jump to: navigation, search
Article Metadata

Tested with
Devices(s): Nokia 5800 XpressMusic, Nokia N900

Compatibility
Platform(s): Qt

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 04:18.
462 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved