Results 1 to 9 of 9

Thread: QSqlQuery::exec: database not open

  1. #1
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Red face QSqlQuery::exec: database not open

    MY CODE :

    QSqlDatabase db;
    QSqlQuery query;

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("sampleDB.db");
    if (db.open())
    {
    qDebug()<<"\n**** db opened**** :";
    query.exec("create table person (id int primary key, "
    "firstname varchar(20), lastname varchar(20))");
    }


    OUTPUT:
    ***db opened***:
    QSqlQuery::exec: database not open


    Why i am getting error even after db.open() succeeded? why Table was not created?

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec: database not open

    When you create QSqlQuery object the default Database must already exist.

    Try with

    Qt Code:
    1.  
    2. db = QSqlDatabase::addDatabase("QSQLITE");
    3. db.setDatabaseName("sampleDB.db");
    4. if (db.open())
    5. {
    6. QSqlQuery query;
    7. qDebug()<<"\n**** db opened**** :";
    8. query.exec("create table person (id int primary key, "
    9. "firstname varchar(20), lastname varchar(20))");
    10. }
    To copy to clipboard, switch view to plain text mode 

    instead of

    Qt Code:
    1. QSqlQuery query;
    2.  
    3. db = QSqlDatabase::addDatabase("QSQLITE");
    4. db.setDatabaseName("sampleDB.db");
    5. if (db.open())
    6. {
    7. qDebug()<<"\n**** db opened**** :";
    8. query.exec("create table person (id int primary key, "
    9. "firstname varchar(20), lastname varchar(20))");
    10. }
    To copy to clipboard, switch view to plain text mode 

    Notice that QSqlQuery is created after QSqlDatabase::addDatabase
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec: database not open

    what was the problem with below code:

    query.exec("create table person (id int primary key, "
    "firstname varchar(20), lastname varchar(20))");
    query.exec("insert into person values(101, 'Danny', 'Young')");
    query.exec("insert into person values(102, 'Christine', 'Holand')");
    query.exec("insert into person values(103, 'Lars', 'Gordon')");
    query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
    query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
    qDebug()<<query.executedQuery();


    Output should be "insert into person values(105, 'Maria', 'Papadopoulos')"
    but the output is "create table person (id int primary key, firstname varchar(20), lastname varchar(20))"

    Why remaining statements(second onwards) not exicuting..? I hope query.executedQuery() function gives recently exicuted query. The above example is from qt creator. Need explination please.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec: database not open

    The function QSqlQuery::executedQuery() returns the last successfully executed query.

    You have probably an error executing insert queries.
    Test the return values of QSqlQuery::exec
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec: database not open

    yes yes..... all insert queries (query.exec("insert....")) returning success status(true), but output not coming as expected. its not printing last successfully executed query.

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec: database not open

    can you post your code?
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec: database not open

    Found answer...

    QSqlQuery::executedQuery()--->returning first successfully executed query.(correct me if i am wrong)
    If i use QSqlQuery::clear() after exicuting each QSqlQuery::exec() then only its giving last suuceessfully exicuted query.

    Why this is happening.. documents in qt were telling some thing other than realty..

  8. #8
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec: database not open

    Quote Originally Posted by newtowindows View Post
    Found answer...
    Why this is happening.. documents in qt were telling some thing other than realty..
    It's Can be a Bug?
    A camel can go 14 days without drink,
    I can't!!!

  9. #9
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec: database not open

    Maybe its a bug. Any expert's views are helpfull for us

Similar Threads

  1. [QSqlQuery] Problem with not open database
    By xkazielx in forum Newbie
    Replies: 11
    Last Post: 1st September 2013, 20:32
  2. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  3. Replies: 4
    Last Post: 23rd November 2010, 17:15
  4. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  5. Best way to open database
    By estanisgeyer in forum Qt Programming
    Replies: 2
    Last Post: 8th February 2008, 05:18

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.