Results 1 to 10 of 10

Thread: Multiple database connections

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows
    Thanks
    1

    Default Re: Multiple database connections

    So, I don't really want to rearrange the entire system to make this happen.

    My current solution is simply to use 2 seperate queries. I SELECTed all of the relevant records from the memory-backed cacheDb_, and then in a single transaction, I am INSERTing them into the table in the disk-backed sessionDb_. I am assuming that this is slower than doing the whole thing in a single SQL command, but it is working for the moment.

    However, I would still be interested in figuring out how to move data between two databases without having to do it one record at a time.

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    13
    Thanked 59 Times in 52 Posts

    Default Re: Multiple database connections

    I still don't understand why you can't use transactions. A SELECT statement will still return up-to-date data, even in the middle of a transaction. A disk database with transactions will easily support 10,000 simple inserts a second, so you could skip the in-memory database. Anyway, here is a sample of using ATTACH. I doubt it's much faster than doing it in Qt, but it is a bit more elegant.
    Qt Code:
    1. void setupDiskDb()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "disk");
    4. db.setDatabaseName("CatchyName");
    5. db.open();
    6.  
    7. QSqlQuery query(db);
    8. query.exec("CREATE TABLE T1(F1, F2, F3)");
    9. }
    10.  
    11. void doSqLiteTest2()
    12. {
    13. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "mem");
    14. db.setDatabaseName(":memory:");
    15. db.open();
    16.  
    17. QSqlQuery query(db);
    18. query.exec("CREATE TABLE T1(F1, F2, F3)");
    19.  
    20. query.prepare("INSERT INTO T1 (F1, F2, F3) "
    21. "VALUES (:1, :2, :3)");
    22.  
    23. for (int i = 0; i < 50000; i++)
    24. {
    25. query.bindValue(":1", i + 0);
    26. query.bindValue(":2", i + 1);
    27. query.bindValue(":3", i + 2);
    28. query.exec();
    29. }
    30.  
    31. qDebug() << query.exec("ATTACH DATABASE 'CatchyName' AS Catchy");
    32. qDebug() << query.exec("INSERT INTO Catchy.T1(F1, F2, F3) SELECT F1, F2, F3 FROM T1");
    33. }
    34.  
    35. int main(int argc, char * argv[])
    36. {
    37. QApplication a(argc, argv);
    38.  
    39. setupDiskDb();
    40. doSqLiteTest2();
    41.  
    42. return a.exec();
    43. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QSqlite, multiple connections to in memory database.
    By adzajac in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2010, 23:35
  2. Multiple connections with QTcpSockets
    By DrDonut in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2009, 11:58
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 17:56
  4. Qt on X11 with multiple display connections?
    By grenoble in forum Qt Programming
    Replies: 1
    Last Post: 25th February 2008, 13:44
  5. Multiple connections to one method
    By davisjamesf in forum Newbie
    Replies: 4
    Last Post: 16th November 2007, 21:11

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
  •  
Qt is a trademark of The Qt Company.