Inserting a row into a database in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Overview[edit | edit source]

This example shows you how to insert data into an SQLite database in Qt. We show how to use an autoincrement field that generates a new ID for the new row automatically.

The table 'person' has the following columns:

  • 76ytuiytuityutyutututyutyutyu, this is an autoincrement field
  • 76ytuiytuityutyutututyutyutyu
  • 76ytuiytuityutyutututyutyutyu
  • 76ytuiytuityutyutututyutyutyu


Preconditions[edit | edit source]

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

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


Header[edit | edit source]

  1. include <QObject>
  2. include <QSqlDatabase>
  3. include <QSqlError>
  4. include <QSqlQuery>

class DatabaseManager : public QObject

   {
   public:
       DatabaseManager(QObject *parent = 0);
       ~DatabaseManager();
       
   public:
       bool openDB();
       bool createPersonTable();
       int insertPerson(QString firstname, QString lastname, int age);
   private:
       QSqlDatabase db;
   };


Source[edit | edit source]

The following code inserts a new row (person) and the autoincrement field generates a new ID for the row. Next, the newly generated ID is queried. int DatabaseManager::insertPerson(QString firstname, QString lastname, int age)

   {
   int newId = -1;
   bool ret = false;
   if (db.isOpen())
       {
       //http://www.sqlite.org/autoinc.html
       // NULL = is the keyword for the autoincrement to generate next value
       
       QSqlQuery query;
       ret = query.exec(QString("insert into person values(NULL,'%1','%2',%3)")
       .arg(firstname).arg(lastname).arg(age));
       // Get database given autoincrement value
       if (ret)
           {
           // http://www.sqlite.org/c3ref/last_insert_rowid.html  
           newId = query.lastInsertId().toInt();
           }
       
       }
   return newId;
   }


The rest of the code: 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;
   }

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[edit | edit source]

A new person is added into the database.


See also[edit | edit source]