Results 1 to 10 of 10

Thread: Multiple database connections

Threaded View

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

    Default Multiple database connections

    I'm building an app that is using a memory-backed database for speed (the database needs to handle at least 60 inserts/second, and I can't batch them). Periodically, I need to copy all of the records from the memory-backed database to a disk-backed one.

    I assumed that I could open two connections, and then use fully-qualified names to move around the records. My code looks something like this (simplified, so it may not compile). Both databases contain behavioraldata tables with xpos,ypos, and time columns:

    Qt Code:
    1. //Open disk-backed database
    2. sessionDb_ = QSqlDatabase::addDatabase("QSQLITE",databaseName);
    3. sessionDb_.setDatabaseName(QCoreApplication::applicationDirPath() + "/" + databaseName + ".sqlite");
    4. Q_ASSERT(sessionDb_.open());
    5.  
    6. //Open memory-backed database
    7. cacheDb_ = QSqlDatabase::addDatabase("QSQLITE",cacheDatabaseName);
    8. cacheDb_.setDatabaseName(":memory:");
    9. Q_ASSERT(cacheDb_.open());
    10.  
    11. // .....Useful code removed.....
    12.  
    13. //Try to copy the behavioraldata table from the memory database to the disk database
    14. QSqlQuery flushQ(cacheDb_ );
    15. QString queryStr = QString("INSERT INTO %1.behavioraldata(xpos,ypos,time) "
    16. "SELECT xpos, ypos, time "
    17. "FROM behavioraldata")
    18. .arg(sessionDb_.connectionName());
    19. flushQ.exec(queryStr);
    20.  
    21.  
    22. //open memory-backed database
    To copy to clipboard, switch view to plain text mode 

    However, when I execute this code, the query fails because it can't find the destination table. Since it wasn't entirely clear what the database "name" was, I tried using both the connection name and the file name, but neither works.

    Does anyone have any idea what I'm doing wrong here? Does SQLITE/QSqlDatase not handle multiple databases? Am I screwing up the
    Last edited by Matt31415; 2nd June 2010 at 17:30.

Similar Threads

  1. QSqlite, multiple connections to in memory database.
    By adzajac in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2010, 22:35
  2. Multiple connections with QTcpSockets
    By DrDonut in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2009, 10:58
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  4. Qt on X11 with multiple display connections?
    By grenoble in forum Qt Programming
    Replies: 1
    Last Post: 25th February 2008, 12:44
  5. Multiple connections to one method
    By davisjamesf in forum Newbie
    Replies: 4
    Last Post: 16th November 2007, 20: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.