×
Namespaces

Variants
Actions
(Difference between revisions)

Creating a database table in Qt

From Nokia Developer Wiki
Jump to: navigation, search
tepaa (Talk | contribs)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(18 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{CodeSnippet
+
{{ArticleMetaData <!-- v1.2 -->
|id=
+
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
|platform=Qt
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices=Nokia 5800 eXpressMusic, Nokia N900
+
|devices= Nokia 5800 XpressMusic, Nokia N900
|category=Qt
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|subcategory=Database
+
|platform= Qt
|creationdate=September 9, 2009
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|keywords=QSqlDatabase, QSQlite, QSqlError, QSqlQuery  
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= QSqlDatabase, QSQlite, QSqlError, QSqlQuery
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090909
 +
|author= [[User:Tepaa]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= Database
 +
|id= CS001505
 
}}
 
}}
  
 
==Overview==
 
==Overview==
  
This example shows you how to create 'person' table into (SQLite) database in Qt.
+
This example shows you how to create a 'person' table in an SQLite database in Qt.
  
Table person has following columns
+
The table 'person' has the following columns:
* id (integer primary key), <b>this is autoincrement field</b> http://www.sqlite.org/autoinc.html
+
* id (integer primary key), <b>this is an autoincrement field</b> http://www.sqlite.org/autoinc.html
 
* firstname (varchar(20))
 
* firstname (varchar(20))
 
* lastname (varchar(30))
 
* lastname (varchar(30))
Line 22: Line 38:
 
==Preconditions==
 
==Preconditions==
  
* Qt is installed on your platform
+
* Qt is installed on your platform.
 +
** S60:
 +
*** Download Qt release from here: [http://developer.qt.nokia.com/wiki/Support_for_Symbian Qt pre-release]
 +
*** Install Qt: [[How to Install Qt]]
 +
*** Check this link for installation guide: [http://developer.qt.nokia.com/wiki/Support_for_Symbiandoc/install-s60.html How to install the package]
 +
*** Qt Tower release has SQLite support. The required libraries are built into the Qt release.
 +
** Maemo:
 +
*** More information about Qt on Maemo can be found here: [http://qt4.garage.maemo.org/ Qt4 Maemo port]
  
** S60
 
*** Download Qt for S60 release from here: [http://pepper.troll.no/s60prereleases/ Qt for S60 pre-release]
 
*** Install Qt for S60: [[Installing Qt on S60]]
 
*** Check this link for installation guide: [http://pepper.troll.no/s60prereleases/doc/install-s60.html How to install the package].
 
*** Qt for S60 Tower release has SQLite support. Needed libraries are bilt in the Qt release.
 
 
 
** Maemo
 
*** More information about Qt on Maemo can be found here: [http://qt4.garage.maemo.org/ Qt4 Maemo port]
 
  
Maemo SQLite development needs following packages to be installed:
+
For Maemo SQLite development, the following packages must be installed:
 
* libqt4-sql
 
* libqt4-sql
* libqt4-sql-sqlit
+
* libqt4-sql-sqlite
 
* libsqlite3-0
 
* libsqlite3-0
 
* libsqlite3-dev
 
* libsqlite3-dev
Line 43: Line 57:
 
==Header==
 
==Header==
  
<code cpp>
+
<code cpp-qt>
 
#include <QObject>
 
#include <QObject>
 
#include <QSqlDatabase>
 
#include <QSqlDatabase>
Line 67: Line 81:
 
==Source==
 
==Source==
  
Create "person" table.
+
Create the 'person' table.
<code cpp>
+
<code cpp-qt>
 
bool DatabaseManager::createPersonTable()
 
bool DatabaseManager::createPersonTable()
 
     {
 
     {
Line 87: Line 101:
 
</code>
 
</code>
  
Rest of the code...
+
The rest of the code:
<code cpp>
+
<code cpp-qt>
 
bool DatabaseManager::openDB()
 
bool DatabaseManager::openDB()
 
     {
 
     {
Line 95: Line 109:
  
 
     #ifdef Q_OS_LINUX
 
     #ifdef Q_OS_LINUX
     // in Maemo we create database into memory because
+
     // NOTE: We have to store database file into user home folder in Linux
     // it does not work from file
+
     QString path(QDir::home().path());
     db.setDatabaseName(":memory:");
+
     path.append(QDir::separator()).append("my.db.sqlite");
 +
    path = QDir::toNativeSeparators(path);
 +
    db.setDatabaseName(path);
 
     #else
 
     #else
     // Set database to be created into file.
+
     // NOTE: File exists in the application private folder, in Symbian Qt implementation
    // In Symbian file exists in the application private folder
+
 
     db.setDatabaseName("my.db.sqlite");
 
     db.setDatabaseName("my.db.sqlite");
 
     #endif
 
     #endif
Line 112: Line 127:
 
===Postconditions===
 
===Postconditions===
  
Database table "person" created.
+
The database table 'person' is created.
  
  
 
==See also==
 
==See also==
  
* [[Tools:Creating SQLite database in Qt]]
+
* [[Creating an SQLite database in Qt]]
* [[Tools:Creating database table in Qt]]
+
* [[Inserting a row into a database in Qt]]
* [[Tools:Insert row into database in Qt]]
+
* [[Searching for data in a database in Qt]]
 +
* [[Deleting data from a database in Qt]]
 +
* [[Selecting data from a database without using SQL statements in Qt]]
 +
* [[Using QDataWidgetMapper to show data from a database in Qt]]
  
 
* [http://www.w3schools.com/Sql/default.asp w3schools.com/Sql]
 
* [http://www.w3schools.com/Sql/default.asp w3schools.com/Sql]
 
* [http://doc.trolltech.com/4.5/qtsql.html QtSql module]
 
* [http://doc.trolltech.com/4.5/qtsql.html QtSql module]
  
[[Category:Qt]][[Category:Code Examples]][[Category: UI]]
+
[[Category:Qt]][[Category:Code Snippet]][[Category: UI]][[Category:Code Snippet]][[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 04:16, 11 October 2012

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic, Nokia N900
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QSqlDatabase, QSQlite, QSqlError, QSqlQuery
Created: tepaa (09 Sep 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

[edit] Overview

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

The table 'person' has the following columns:


[edit] Preconditions


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

  • libqt4-sql
  • libqt4-sql-sqlite
  • libsqlite3-0
  • libsqlite3-dev


[edit] 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;
};


[edit] 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();
}


[edit] Postconditions

The database table 'person' is created.


[edit] See also

This page was last modified on 11 October 2012, at 04:16.
687 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.

×