Results 1 to 4 of 4

Thread: [SOLVED]: addDatabase with connectionName problem

  1. #1
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default [SOLVED]: addDatabase with connectionName problem

    Hello again!
    I have strange problem while dealing with SQLite database. Everything seem so be all right when I open it like this:
    Qt Code:
    1. QString app_path = QApplication::applicationDirPath();
    2. QString dbase_path = app_path + "/base.bz";
    3. {
    4. QSqlDatabase base = QSqlDatabase::addDatabase("QSQLITE");
    5. base.setDatabaseName(dbase_path);
    6. base.open();
    7. if(base.isOpen() != true){
    8. QMessageBox::critical(this, "Error", "Base is not open.");
    9. }
    10. else {
    11. // the database is opened
    12. // create data table
    13. QSqlQuery sql_create_table("CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, date TEXT, ...)");
    14. ...
    15. }
    16. base.close();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Except when I try to reconnect I get alert:
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    Data is correctly writen and extracted to and from database.
    In docs it is writen, that I must use connectionName to avoid this error. I change:
    Qt Code:
    1. QSqlDatabase base = QSqlDatabase::addDatabase("QSQLITE", "conn_name");
    To copy to clipboard, switch view to plain text mode 
    and add
    Qt Code:
    1. QSqlDatabase::removeDatabase("conn_name");
    To copy to clipboard, switch view to plain text mode 
    after I close the database. The alert now say:
    QSqlQuery::exec: database not open
    , nothing is writen to the database. base.isDriverAvailable say, it is not...query.lastError() say Driver not loaded. However base.isOpen() say it is open!
    What am I doing wrong? I am using Qt 4.5 that came with Fedora 12, sqlite driver is present (at least it was instaled with fedora and in use without connectionName it is working).
    Regards,
    Luka
    Last edited by omci; 10th December 2009 at 13:42. Reason: problem solved

  2. #2
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: addDatabase with connectionName problem

    As You have set a name for Your data base connection You will need to specify a connection for the Query to use.
    QSqlQuery::QSqlQuery ( const QString & query = QString(), QSqlDatabase db = QSqlDatabase() )

    Qt Code:
    1. QSqlQuery qry(QSqlDatabase::database("conn_name"));
    2. qry.prepare("....");
    To copy to clipboard, switch view to plain text mode 
    Last edited by Grimlock; 10th December 2009 at 11:10.
    Me, Grimlock, not "nice dino". ME BASH BRAINS!

  3. The following user says thank you to Grimlock for this useful post:

    omci (10th December 2009)

  4. #3
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED]: addDatabase with connectionName problem

    That solved my problem. Thank you for fast replay.
    Luka

  5. #4
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED]: addDatabase with connectionName problem

    You are welcome.
    Me, Grimlock, not "nice dino". ME BASH BRAINS!

Similar Threads

  1. Replies: 19
    Last Post: 3rd April 2009, 23:17
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.