×
Namespaces

Variants
Actions
(Difference between revisions)

Deleting data from a database in Qt

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix links)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
Line 57: Line 57:
 
==Header==
 
==Header==
  
<code cpp>
+
<code cpp-qt>
 
#include <QObject>
 
#include <QObject>
 
#include <QSqlDatabase>
 
#include <QSqlDatabase>
Line 83: Line 83:
  
 
Delete a person from the database:
 
Delete a person from the database:
<code cpp>
+
<code cpp-qt>
 
bool DatabaseManager::deletePerson(int id)
 
bool DatabaseManager::deletePerson(int id)
 
     {
 
     {
Line 98: Line 98:
  
 
The rest of the code:
 
The rest of the code:
<code cpp>
+
<code cpp-qt>
 
bool DatabaseManager::openDB()
 
bool DatabaseManager::openDB()
 
     {
 
     {

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 delete data from an SQLite database in Qt.

The table 'person' has the following columns:

  • id (integer primary key), this is an autoincrement field
  • firstname (varchar(20))
  • lastname (varchar(30))
  • age (integer)


[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>
#include <QString>
 
class DatabaseManager : public QObject
{
public:
DatabaseManager(QObject *parent = 0);
~DatabaseManager();
 
public:
bool openDB();
bool deletePerson(int id);
 
private:
QSqlDatabase db;
};


[edit] Source

Delete a person from the database:

bool DatabaseManager::deletePerson(int id)
{
bool ret = false;
if (db.isOpen())
{
QSqlQuery query;
ret = query.exec(QString("delete from person where id=%1").arg(id));
}
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

A person's data is deleted from the database.


[edit] See also

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

×