×
Namespaces

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

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Creating a database table 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, QSqlError, QSqlQuery
Created: (09 Sep 2009)
Last edited: tepaa (09 Sep 2009)

Contents

Overview

This example shows you how to create 'person' table into (SQLite) database in Qt.

Table person has following columns

  • id (int primary key)
  • firstname (varchar(20))
  • lastname (varchar(30))
  • age (int)


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 <QObject>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
 
class DatabaseManager : public QObject
{
public:
DatabaseManager(QObject *parent = 0);
~DatabaseManager();
 
public:
bool openDB();
bool createPersonTable();
 
private:
QSqlDatabase db;
};


Source

bool DatabaseManager::createPersonTable()
{
// Create table "person"
bool ret = false;
if (db.isOpen())
{
QSqlQuery query;
ret = query.exec("create table person "
"(id int primary key, "
"firstname varchar(20), "
"lastname varchar(30), "
"age int)");
 
}
return ret;
}
bool DatabaseManager::openDB()
{
// Find QSLite driver
db = QSqlDatabase::addDatabase("QSQLITE");
 
#ifdef Q_OS_LINUX
// in Maemo we create database into memory because
// it does not work from file
db.setDatabaseName(":memory:");
#else
// Set database to be created into file.
// In Symbian file exists in the application private folder
db.setDatabaseName("my.db.sqlite");
#endif
 
// Open databasee
return db.open();
}


Postconditions

Database table "person" created.


See also

666 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.

×