Creating a database table in Qt
hamishwillee
(Talk | contribs) m (Hamishwillee - Bot update - Merge KB into wiki) |
hamishwillee
(Talk | contribs) m (Text replace - "<code cpp>" to "<code cpp-qt>") |
||
| (3 intermediate revisions by one user not shown) | |||
| Line 57: | Line 57: | ||
==Header== | ==Header== | ||
| − | <code cpp> | + | <code cpp-qt> |
#include <QObject> | #include <QObject> | ||
#include <QSqlDatabase> | #include <QSqlDatabase> | ||
| Line 82: | Line 82: | ||
Create the 'person' table. | Create the 'person' table. | ||
| − | <code cpp> | + | <code cpp-qt> |
bool DatabaseManager::createPersonTable() | bool DatabaseManager::createPersonTable() | ||
{ | { | ||
| Line 102: | Line 102: | ||
The rest of the code: | The rest of the code: | ||
| − | <code cpp> | + | <code cpp-qt> |
bool DatabaseManager::openDB() | bool DatabaseManager::openDB() | ||
{ | { | ||
| Line 133: | Line 133: | ||
* [[Creating an SQLite database in Qt]] | * [[Creating an SQLite database in Qt]] | ||
| − | * [[ | + | * [[Inserting a row into a 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 Snippet]][[Category: UI]][[Category:Code Snippet]][[Category:MeeGo]] [[Category:Symbian]] | + | [[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
Article
Keywords: QSqlDatabase, QSQlite, QSqlError, QSqlQuery
Created: tepaa
(09 Sep 2009)
Last edited: hamishwillee
(11 Oct 2012)
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:
- id (integer primary key), this is an autoincrement field http://www.sqlite.org/autoinc.html
- firstname (varchar(20))
- lastname (varchar(30))
- age (integer)
Preconditions
- Qt is installed on your platform.
- S60:
- Download Qt release from here: Qt pre-release
- Install Qt: How to Install Qt
- Check this link for installation guide: 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: Qt4 Maemo port
- S60:
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.

