Results 1 to 10 of 10

Thread: Multiple database connections

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Multiple database connections

    You can indeed use the attach command. Did you already try it?
    http://www.sqlite.org/lang_attach.html

    Now that I now a bit more about your program, why don't you display the data directly to the experimenter without going through the database first? Or did you already do that? At least, this should eliminate any lag from the database. The database can then be used as a "tape recorder".

    Edit: ohh and sorry for the indirect questions to your question. I'm trying to understand what you want to do and why.

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

    Default Re: Multiple database connections

    I'm actually using the addDatabase command since it is just as easy, and works the same way. At least according to this:

    http://www.qtcentre.org/threads/2527...TTACH-database

    Because it's possible that multiple experimenters will be viewing the experiment, I need to route everything through the server. I could use some sort of simple data structure to store it all, but since I was already using the database, it made sense to store everything there (also this means that if I decide to change the schema, I can simply change the tables in the database).

    If it turns out that there is no way to do this with a single SQL statement, I can read everything out and write it back by hand, but this will likely be slower.

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Multiple database connections

    With the attach command, you can use one query. I didn't know about that.
    You do need to use the actual database name, as in filename, when using the attach command. The connection name is a way for the Qt classes to make a difference between multiple databases.

    Here's what I would do though (see attachment)
    There's a direct connection between the main experimenter (experimenter 1) and the subject. Whatever the subject does with his mouse, the data is transfered and displayed in real time on the computer of experimenter 1. The server (running on the computer of experimenter 1 for example, but can be stand alone too) directly sends the data to the connected clients (as in a proxy) in real time. The other experimenters see the data in real time too (well, at least with a very minor lag, there's always some overhead). As a last step, the data is cached for a second or two and then written to the database in the background. The database is only used to record everything, not to display (unless at a later time).
    Attached Images Attached Images

  4. #4
    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.

  5. #5
    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.