PDA

View Full Version : No query Unable to fetch row when ERROR , when trying to do a simple query



Being friendzoned sucks
15th April 2015, 12:00
I have a sqlite 3 database where I have some fake data just for testing, but it seems that every time I try to do a simple query it gives me the error that I mentioned in the title.

here's the code:


QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("adtDB.sql");
db.open();
if(db.isOpen())
{
QSqlQuery q = db.exec("SELECT * FROM adt");
if(q.exec())
{
qDebug()<<"works!";
while(q.next())
{
qDebug()<<q.value(8).toString();
}
}
qDebug()<<"---db failed to open! , error: "<<q.lastError().text();
db.close();
return true;
}
qDebug()<<"db failed to open! , error: "<<db.lastError().text();
return false;

The database opens fine but this is the error that I get including the debug statement:
---db failed to open! , error: "No query Unable to fetch row"

the red text is the error the rest is in the debug statement that I included.

Lesiok
15th April 2015, 12:11
You are doing 2 times exec(). Code should looks like :

QSqlQuery q = db.exec("SELECT * FROM adt");
if(!q.lastError().isValid())
{
qDebug()<<"works!";
while(q.next())
{
qDebug()<<q.value(8).toString();
}
}
else
{
qDebug()<<"---db failed to open! , error: "<<q.lastError().text();
}
db.close();
return true;

Being friendzoned sucks
15th April 2015, 12:24
You are doing 2 times exec(). Code should looks like :

QSqlQuery q = db.exec("SELECT * FROM adt");
if(!q.lastError().isValid())
{
qDebug()<<"works!";
while(q.next())
{
qDebug()<<q.value(8).toString();
}
}
else
{
qDebug()<<"---db failed to open! , error: "<<q.lastError().text();
}
db.close();
return true;

Thanks for your answer but this gives me another error:

file is encrypted or is not a database Unable to execute statement

new code:


QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("adtDB.sql");
db.open();
if(db.isOpen())
{
QSqlQuery q = db.exec("SELECT * FROM adt");
if(!q.lastError().isValid())
{
qDebug()<<"works!";
while(q.next())
{
qDebug()<<q.value(8).toString();
}
}
qDebug()<<"---db failed to open! , error: "<<q.lastError().text();
db.close();
return true;
}
qDebug()<<"db failed to open! , error: "<<db.lastError().text();
return false;

Being friendzoned sucks
15th April 2015, 15:07
Sorry for commenting so the post will go to the top, but this problem is pretty annoying and I really need a fix since I need to finish this in two days to submit it.

Lesiok
15th April 2015, 18:47
From which line is this error : 15 or 19 ? Description of error is clear. The database is encrypted or damaged.

Being friendzoned sucks
16th April 2015, 10:10
It's from line 15

Lesiok
16th April 2015, 10:51
Line 15 is pointless. Compare my code and Your.

jefftee
16th April 2015, 17:11
Can you access this database using the sqlite3 command line program and execute your query successfully? If not, then your Qt program certainly won't be able to either.

Since you are specifying a relative file name for your database, you may be creating an empty file inadvertently by opening your database in a different working directory than you think.

Try hard coding the fully qualified file path and see if that works and/or search your drive for your file name and see if you have another file created somewhere you did not intend, etc.