Results 1 to 6 of 6

Thread: two databases opened at the same time

  1. #1
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default two databases opened at the same time

    Hi!

    Me again with my database based problems.
    I have opened two databases in order to transfer data from one database to another. I declare first query as:
    Qt Code:
    1. QSqlQuery query_read("SELECT * FROM groups", db1);
    2. int is_kat = query_read.record().indexOf("group");
    3. while(query_read.next()) {
    4. QString s_kat = query_read.value(is_kat).toString();
    5. //some code + declaration for another query
    6. }
    To copy to clipboard, switch view to plain text mode 
    The second query is declared inside the marked area like this:
    Qt Code:
    1. QSqlQuery query_write(db2);
    2. query_write.prepare("INSERT INTO kat (kategories) VALUES (:kategorie)");
    3. query_write.bindValue(":kategorija", s_kat);
    4. query_write.exec();
    To copy to clipboard, switch view to plain text mode 
    Compiling does not return any error, but the data is not transfered. Where is the error?

    Regards,
    Luka

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: two databases opened at the same time

    ":kategorie" and ":kategorija" -- they should be the same.

  3. #3
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Red face Re: two databases opened at the same time

    tnx... I'm ashamed... i stared for two hours at the screen but i never see that error... now it works fine!

    tnx again!
    Luka

  4. #4
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: two databases opened at the same time

    I try to open two seperate SQLite databases at the same time using code below. Why do I get message that the first DB is not opened?
    Qt Code:
    1. QSqlDatabase base1 = QSqlDatabase::addDatabase("QSQLITE");
    2. QString pot = ui.p_odpri->text();
    3. base1.setDatabaseName(pot);
    4. base1.open();
    5. QSqlDatabase base2 = QSqlDatabase::addDatabase("QSQLITE");
    6. base2.setDatabaseName(qApp->applicationDirPath() + "/base.db");
    7. base2.open();
    8. if (base1.isOpen() != TRUE) {
    9. QMessageBox::critical(this, "Dnevnik", "Error opening DB 1");
    10. }
    11. else {
    12. if(base2.isOpen() != TRUE){
    13. QMessageBox::critical(this, "Dnevnik", "Error opening DB 2");
    14. }
    15. else {
    16. //some code to execute
    17. }
    18. }
    19. base2.close();
    20. base1.close();
    To copy to clipboard, switch view to plain text mode 
    If I use code below everything works just fine...
    Qt Code:
    1. QSqlDatabase base1 = QSqlDatabase::addDatabase("QSQLITE");
    2. QString pot = ui.p_odpri->text();
    3. base1.setDatabaseName(pot);
    4. base1.open();
    5. if (base1.isOpen() != TRUE) {
    6. QMessageBox::critical(this, "Dnevnik", "Error opening DB 1");
    7. }
    8. else {
    9. QSqlDatabase base2 = QSqlDatabase::addDatabase("QSQLITE");
    10. base2.setDatabaseName(qApp->applicationDirPath() + "/base.db");
    11. base2.open();
    12. if(base2.isOpen() != TRUE){
    13. QMessageBox::critical(this, "Dnevnik", "Error opening DB 2");
    14. }
    15. else {
    16. //some code to execute
    17. }
    18. base2.close();
    19. }
    20. base1.close();
    To copy to clipboard, switch view to plain text mode 
    Can somebody explain what I do with the first and what with the second code?

    Regards,
    Luka
    Last edited by whoops.slo; 28th January 2006 at 08:49.

  5. #5
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: two databases opened at the same time

    try use QSqlDatabase::lastError, for finding solution

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: two databases opened at the same time

    Quote Originally Posted by whoops.slo
    I try to open two seperate SQLite databases at the same time using code below. Why do I get message that the first DB is not opened?
    From Qt docs:
    QSqlDatabase QSqlDatabase::addDatabase ( const QString & type, const QString & connectionName = QLatin1String( defaultConnection ) ) [static]
    Adds a database to the list of database connections using the driver type and the connection name connectionName. If there already exists a database connection called connectionName, that connection is removed.
    You open the same connection twice.

    Try:
    Qt Code:
    1. QSqlDatabase base1 = QSqlDatabase::addDatabase( "QSQLITE" );
    2. // ...
    3. QSqlDatabase base2 = QSqlDatabase::addDatabase( "QSQLITE", "second_or_whatever" );
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to constantly refresh time on a view
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2008, 12:44
  2. Replies: 1
    Last Post: 1st February 2008, 18:55
  3. QDateTime GMT add sec. or - sec. from locale time....
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2007, 16:39
  4. Problem with pointers while using localtime() and time()
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 11th January 2006, 15:48

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.