PDA

View Full Version : QSqlQuery::exec: database not open



newtowindows
28th October 2009, 14:54
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?

mcosta
28th October 2009, 16:38
When you create QSqlQuery object the default Database must already exist.

Try with



QSqlDatabase db;

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


instead of



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))");
}


Notice that QSqlQuery is created after QSqlDatabase::addDatabase

newtowindows
28th October 2009, 18:24
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.

mcosta
28th October 2009, 18:34
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

newtowindows
28th October 2009, 18:45
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.

mcosta
28th October 2009, 19:03
can you post your code?

newtowindows
28th October 2009, 19:16
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..

mcosta
29th October 2009, 08:01
Found answer...
Why this is happening.. documents in qt were telling some thing other than realty..

It's Can be a Bug?

newtowindows
29th October 2009, 08:48
Maybe its a bug. Any expert's views are helpfull for us