Results 1 to 5 of 5

Thread: Execute sql queries on multiple databases

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Execute sql queries on multiple databases

    I want execute queries on my main and backup database, how do i do it? I currently have two connections running until I go to execute a query, I get the error database not open. I'm open to suggestions outside of this program to maintaining an efficient contemporaneous backup system.

    My code:
    Qt Code:
    1. {
    2. ui->setupUi(this);
    3. //Setup DB connection
    4. db2 = QSqlDatabase::addDatabase("QSQLITE", "main");
    5. db2.setDatabaseName(Path_to_DB);
    6. QFileInfo checkFile(Path_to_DB);
    7.  
    8. if(checkFile.isWritable())
    9. {
    10. if(db2.open())
    11. {
    12. qDebug() << "Connected to database file";
    13. }
    14. }else{
    15. qDebug() << "Database file not found";
    16. }
    17.  
    18. dbb = QSqlDatabase::addDatabase("QSQLITE", "backup");
    19. dbb.setDatabaseName(Path_to_DBBackup);
    20. QFileInfo checkFile2(Path_to_DBBackup);
    21.  
    22. if(checkFile2.isWritable())
    23. {
    24. if(dbb.open())
    25. {
    26. qDebug() << "Connected to database backup file";
    27. }
    28. }else{
    29. qDebug() << "Database file not found";
    30. }
    31.  
    32. QString email = ui->email->text();
    33. QString q = QString("SELECT * FROM users WHERE email = '%1'").arg(email);
    34. QSqlQuery qry;
    35. if(qry.exec(q))
    36. {
    37. if (qry.next())
    38. {
    39. return false;
    40. }
    41. }
    42. else{qDebug() << "Query unsuccessful!";}
    43. }
    To copy to clipboard, switch view to plain text mode 

    Everything works when I query just one db...

    Thanks in advance.

  2. #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: Execute sql queries on multiple databases

    You are creating a QSqlQuery [line 34] operating on the default connection, which is neither of the two connections your code opens.

  3. #3
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Execute sql queries on multiple databases

    Can you show me how to query multiple databases in qt?
    Last edited by Cyrebo; 19th April 2013 at 23:05.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Execute sql queries on multiple databases

    Defining QSqlQuery you specify the database to which it relates, eg
    Qt Code:
    1. QSqlQuery qry(db2);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Execute sql queries on multiple databases

    This thread is solved now thanks.

Similar Threads

  1. Replies: 1
    Last Post: 10th September 2012, 18:56
  2. Multiple Queries populating QSqlQueryModel
    By hollyberry in forum Newbie
    Replies: 1
    Last Post: 24th April 2012, 02:36
  3. Multiple open databases and MVC
    By mtnbiker66 in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2012, 01:08
  4. how to execute multiple commands at a time
    By PHANI in forum Qt Programming
    Replies: 4
    Last Post: 22nd February 2012, 10:01
  5. Multiple sql queries
    By fantom in forum Qt Programming
    Replies: 3
    Last Post: 28th February 2011, 18:00

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.