×
Namespaces

Variants
Actions
(Difference between revisions)

Creating a database table in Qt

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Bot fixing redirect link)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
Line 1: Line 1:
 
{{KBCS}}
 
{{KBCS}}
{{CodeSnippet
+
{{ArticleMetaData
 
|id=CS001505
 
|id=CS001505
 
|platform=Qt
 
|platform=Qt
Line 8: Line 8:
 
|creationdate=November 24, 2009
 
|creationdate=November 24, 2009
 
|keywords=QSqlDatabase, QSQlite, QSqlError, QSqlQuery  
 
|keywords=QSqlDatabase, QSQlite, QSqlError, QSqlQuery  
 +
 +
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
 +
|author=[[User:Tepaa]]
 
}}
 
}}
  

Revision as of 12:11, 24 June 2011

Template:KBCS

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic, Nokia N900
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QSqlDatabase, QSQlite, QSqlError, QSqlQuery
Created: tepaa (24 Nov 2009)
Last edited: hamishwillee (24 Jun 2011)

Contents

Overview

This example shows you how to create a 'person' table in an SQLite database in Qt.

The table 'person' has the following columns:


Preconditions


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

  • libqt4-sql
  • libqt4-sql-sqlite
  • 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

Create the 'person' table.

bool DatabaseManager::createPersonTable()
{
// Create table "person"
bool ret = false;
if (db.isOpen())
{
QSqlQuery query;
ret = query.exec("create table person "
"(id integer primary key, "
"firstname varchar(20), "
"lastname varchar(30), "
"age integer)");
 
}
return ret;
}

The rest of the code:

bool DatabaseManager::openDB()
{
// Find QSLite driver
db = QSqlDatabase::addDatabase("QSQLITE");
 
#ifdef Q_OS_LINUX
// NOTE: We have to store database file into user home folder in Linux
QString path(QDir::home().path());
path.append(QDir::separator()).append("my.db.sqlite");
path = QDir::toNativeSeparators(path);
db.setDatabaseName(path);
#else
// NOTE: File exists in the application private folder, in Symbian Qt implementation
db.setDatabaseName("my.db.sqlite");
#endif
 
// Open databasee
return db.open();
}


Postconditions

The database table 'person' is created.


See also

709 page views in the last 30 days.
×