Guest User

Metalab QT Workshop

a guest
Mar 23rd, 2011
189
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×