Want more features on Pastebin? Sign Up, it's FREE!
Guest

Metalab QT Workshop

By: a guest on Mar 23rd, 2011  |  syntax: None  |  size: 1.19 KB  |  views: 141  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <QSqlDatabase>
  2. #include <QSqlQuery>
  3. #include <QVariant>
  4. #include <stdio.h>
  5.  
  6. int main(int argc, char * argv[])
  7. {
  8.     Q_UNUSED(argc);
  9.     Q_UNUSED(argv);
  10.  
  11.     /** Connect to database */
  12.     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  13.  
  14.     /** Set database name (and login for other db drivers) */
  15.     db.setDatabaseName(":memory:");
  16.     //db.setDatabaseName("test.db");
  17.  
  18.     /** Open database */
  19.     if (!db.open())
  20.     {
  21.         return 1;
  22.     }
  23.  
  24.     /* Do some queries with the default connectin */
  25.     QSqlQuery query;
  26.  
  27.     query.exec("CREATE TABLE test (id int primary key, name varchar(20))");
  28.  
  29.     query.exec("INSERT INTO test VALUES (1, 'Name 1')");
  30.     query.exec("INSERT INTO test VALUES (2, 'Name 2')");
  31.     query.exec("INSERT INTO test VALUES (3, 'Name 3')");
  32.     query.prepare("INSERT INTO test VALUES (4, :bindval)");
  33.     query.bindValue(":bindval", "Name 4");
  34.     query.exec();
  35.  
  36.     query.exec("SELECT * FROM test WHERE id > '2'");
  37.  
  38.     while (query.next())
  39.     {
  40.         QString name = query.value(1).toString();
  41.         printf("Name: %s\n", name.toLatin1().constData());
  42.     }
  43.  
  44.     /** Database closed in destructor */
  45.     return 0;
  46. }
clone this paste RAW Paste Data