Results 1 to 6 of 6

Thread: Sqlite & QSqlDatabase problem

  1. #1
    rsimone Guest

    Default Sqlite & QSqlDatabase problem

    Hi
    I've a question on sqlite and QSqlDatabase class.
    I found the follow example to connect to sqlite db:

    In example.cpp file
    SqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("localhost");
    db.setDatabaseName("test.db");
    if (!db.open()) {
    return true;
    }

    it works but previously I had written the following code:

    In example.h file
    .......
    QSqlDatabase *db;
    ......

    In example.cpp file
    b = new QSqlDatabase();
    DriverList = db->drivers();
    qDebug() << "Driver disponibili: ";
    for (int i = 0; i < DriverList.size() ; ++i)
    qDebug() << DriverList.at(i);
    db->QSqlDatabase::addDatabase("QSQLITE");
    db->setHostName("localhost");
    db->setDatabaseName("test.db");
    if (!db->open()) {
    return true;
    }
    it compiles and it doesn't show any error by running but it don't open any connection to DB

    Why???

    p.s. sorry for my bad english

  2. #2
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite & QSqlDatabase problem

    The following code creates an invalid (empty) QSqlDatabase object:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    To get a valid one you have to call QSqlDatabase::addDatabase.
    Your second example calls this method in a wrong way. You ignore the returned object. So your QSqlDatabase object still be invalid.
    In your first example it's right.

    Btw: Do you want to use this code?
    Qt Code:
    1. if (!db.open()) {
    2. return true;
    3. }
    To copy to clipboard, switch view to plain text mode 
    It seems not logical?! (IMHO return false; would be better )

    Bye

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


  4. #3
    rsimone Guest

    Default Re: Sqlite & QSqlDatabase problem

    Ok

    so if I create an instance of the QSqlDatabase() object:

    QSqlDatabase db = new QSqlDatabase();
    then I can't assign the db driver:

    db->QSqlDatabase::addDatabase("QSQLITE");
    but I must use it so:
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    without creating first the QSqlDatabase object.

    it seems me strange but maybe because I'm a java developer.

    you are right

    if (!db.open()) {
    return true;
    }
    it is wrong

    Bye and thanks

  5. #4
    Join Date
    Jul 2009
    Location
    Aracaju-BR
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite & QSqlDatabase problem

    Nah, this is not stragen, even for a Java Dev. Its just that it QSqlDatabase is/behaves like a singleton (thou the constructor is public). So, you can create a new QSqlDatabase, but it is useless, since its not the one used by the class itself.

  6. The following user says thank you to KhaoticMind for this useful post:


  7. #5
    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: Sqlite & QSqlDatabase problem

    I'd even say that creating an object of this class on heap is an error. This is an explicitly shared class that should be assigned to an object allocated on stack. The database handler needs to be created using a static call to addDatabase() and thus calling db->addDatabase() is an error as well. The correct call is:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase(...);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:


  9. #6
    rsimone Guest

    Default Re: Sqlite & QSqlDatabase problem

    thank you very much

Similar Threads

  1. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 13:05
  2. deployment problem: msvc++ 2008 Express, Qt 4.4.3
    By vonCZ in forum Qt Programming
    Replies: 7
    Last Post: 10th November 2008, 15:38
  3. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 13:48
  4. SQLite Woes
    By morraine in forum Newbie
    Replies: 5
    Last Post: 12th August 2008, 20:27
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

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.