×
Namespaces

Variants
Actions
Revision as of 22:14, 9 September 2009 by valderind4 (Talk | contribs)

Selecting data from a database without using SQL statements in Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 5800 eXpressMusic, Nokia N900
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QSqlDatabase, QSQlite, QSqlTableModel, QSqlRecord
Created: (09 Sep 2009)
Last edited: valderind4 (09 Sep 2009)

Contents

Overview

This example shows you how to search data with QSqlTableModel from (SQLite) database in Qt without using any SQL statements.

Table person has following columns

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


Preconditions

  • Qt is installed on your platform


    • Maemo
      • More information about Qt on Maemo can be found here: Qt4 Maemo port

Maemo SQLite development needs following packages to be installed:

  • libqt4-sql
  • libqt4-sql-sqlit
  • libsqlite3-0
  • libsqlite3-dev


Header

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


Source

Select person data. Person id is given as a filter for the select.

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();
 
// Read result
QSqlRecord record = model->record();
if (!record.isEmpty())
QString name = record.value(2).toString();


Postconditions

Person's data searched form the database without using SQL statements.


See also

209 page views in the last 30 days.