Results 1 to 4 of 4

Thread: multiple in memory sqlite databases with Qt

  1. #1
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default multiple in memory sqlite databases with Qt

    What is the CORRECT syntax for opening multiple SQLite In Memory databases with Qt? The example found here:

    http://www.sqlite.org/inmemorydb.html

    rc = sqlite3_open("file:memdb1?mode=memory&cache=shared ", &db);

    for a name string does not work.

    Thanks,
    Roland

  2. The following user says thank you to RolandHughes for this useful post:


  3. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: multiple in memory sqlite databases with Qt

    You do it exactly the same way you handle multiple database connections to other databases.
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QSqlDatabase>
    3. #include <QSqlQuery>
    4. #include <QStringList>
    5. #include <QDebug>
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. QCoreApplication app(argc, argv);
    10.  
    11. QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE", "conn1");
    12. db1.setDatabaseName(":memory:");
    13. if (db1.open()) {
    14. qDebug() << "db1 open";
    15. QSqlQuery qry("CREATE TABLE test1 (a integer)", db1);
    16. qDebug() << "Tables in db1" << db1.tables();
    17. }
    18.  
    19. QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE", "conn2");
    20. db2.setDatabaseName(":memory:");
    21. if (db2.open()) {
    22. qDebug() << "db2 open";
    23. QSqlQuery qry("CREATE TABLE test2 (a integer)", db2);
    24. qDebug() << "Tables in db2" << db2.tables();
    25. }
    26.  
    27.  
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  4. The following user says thank you to ChrisW67 for this useful post:


  5. #3
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: multiple in memory sqlite databases with Qt

    I will try it, but according to the SQLite link I posted :memory: only opens a single database, i.e. the second request is going to open the SAME database on a different connection. I need to open multiple memory databases on different connections. Each database has completely different content.

  6. The following user says thank you to RolandHughes for this useful post:


  7. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: multiple in memory sqlite databases with Qt

    I you bothered to run my example code you would find that they are two, independent in-memory databases with different tables. This is exactly as described in the Sqlite page you linked:
    Every :memory: database is distinct from every other. So, opening two database connections each with the filename ":memory:" will create two independent in-memory databases.

Similar Threads

  1. Execute sql queries on multiple databases
    By Cyrebo in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2013, 22:32
  2. Replies: 1
    Last Post: 10th September 2012, 18:56
  3. Multiple open databases and MVC
    By mtnbiker66 in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2012, 01:08
  4. AES-256 encrypted SQLite databases with SQLCipher
    By Lykurg in forum Installation and Deployment
    Replies: 10
    Last Post: 29th July 2011, 04:22
  5. Loading and saving in-memory SQLITE databases
    By miraks in forum Qt Programming
    Replies: 10
    Last Post: 27th April 2010, 22:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.