QSqlQuery::isValid returns false
hello,
i am accessing a ms access database and i have the folowing problem.
if(query->isSelect())
QMessageBox::warning(this, "title", "select true"); //always returns true
query->last();
if(query->isValid())
QMessageBox::warning(this, "title", "true"); // always return false
when i execute last() function it does not return the last record and the query position is set to an invalid position.
why when i execute the last() function it does not return the required record?
and why it points to an invalid position?
thank you
Re: QSqlQuery::isValid returns false
Re: QSqlQuery::isValid returns false
Quote:
What does QSqlQuery::isActive() return?
i tried it before and after the QSqlQuery::last()
and it always returns true
Re: QSqlQuery::isValid returns false
Can you provide the whole block of code used to operate on the query?
Re: QSqlQuery::isValid returns false
that is the code i use
Code:
#include "shamelareader.h"
shamelaReader
::shamelaReader(QWidget *parent
){
setCentralWidget(mainText);
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=c:\\book.mdb");
db.open();
createActions();
createToolBar();
query->exec("SELECT name FROM employee");
query->next();
mainText->setText(query->value(0).toString());
}
void shamelaReader::createActions()
{
nextAct
= new QAction(tr
("&Next"),
this);
connect(nextAct, SIGNAL(triggered()), this, SLOT(next()));
prevAct
= new QAction(tr
("&previous"),
this);
connect(prevAct, SIGNAL(triggered()), this, SLOT(prev()));
firstAct
= new QAction(tr
("first"),
this);
connect(firstAct, SIGNAL(triggered()), this, SLOT(first()));
lastAct
= new QAction(tr
("last"),
this);
connect(lastAct, SIGNAL(triggered()), this, SLOT(last()));
}
void shamelaReader::createToolBar()
{
toolBar = addToolBar(tr("Navigation"));
toolBar->addAction(firstAct);
toolBar->addAction(nextAct);
toolBar->addAction(prevAct);
toolBar->addAction(lastAct);
}
void shamelaReader::next()
{
query->next();
mainText->setText(query->value(0).toString());
}
void shamelaReader::prev()
{
query->previous();
mainText->setText(query->value(0).toString());
}
void shamelaReader::first()
{
query->first();
mainText->setText(query->value(0).toString());
}
void shamelaReader::last()
{
query->last();
if(query->isActive())
if(query->isValid())
mainText->setText(query->value(0).toString());
}
Re: QSqlQuery::isValid returns false
What does query->last() return? Maybe the driver only supports sequential access? Have you verified that?
Re: QSqlQuery::isValid returns false
query->last()
return true
Quote:
Maybe the driver only supports sequential access? Have you verified that?
how can i know that the driver does not support sequential access
Re: QSqlQuery::isValid returns false
if last() returns true, then it means the query has been positioned correctly.
Code:
if(query->last(){
qDebug("%d", rec.count());
}
What does this output? Remember to have console enabled for your project or else you won't see anything.
Re: QSqlQuery::isValid returns false
excuse me but i dont know how to enable the console.
i am using visual studio 2005.
i hope, i am not wasting your time.
but i believe if i want to learn some thing i must start my project directly as sayed in Qt documentation .but on the road there are missed things.
thank you
Re: QSqlQuery::isValid returns false
If you're using a commercial licence then access your project options using the integration plugin and it should be there somewhere. If not, run your application from within VS debugger and it should display the message. As an alternative, prepare a small qmake project and include a CONFIG+=console line in it.
Re: QSqlQuery::isValid returns false
i made as you said and i get the following output:
1
QSqlQuery::value: not positioned on a valid record
Re: QSqlQuery::isValid returns false
How many records does the table hold?
Re: QSqlQuery::isValid returns false
i tried it with two databases
one of them contains three records
and the other one contains 90 records.
Re: QSqlQuery::isValid returns false
Could you prepare a minimal compilable example reproducing the problem?
Re: QSqlQuery::isValid returns false
here, it is the source code .
http://www.filesend.net/download.php...df51c62f649b7c
please dont forget to put the sample database in the ( c:\ ) directory.
the database are contained in the Zip file and also the *.pro file.
Re: QSqlQuery::isValid returns false
i tried the same code with Sqlite database and the program works fine.
but why does not it work with MS access?
Re: QSqlQuery::isValid returns false
As I (think I) already said, maybe Access only supports sequential access to data.
Re: QSqlQuery::isValid returns false
do you mean that i can not deal with access databases with Qt?
Re: QSqlQuery::isValid returns false
No, I mean Access databases can't jump across the result set regardless if you use Qt or not. If you want to reach the last entry, you have to iterate each record one by one until you reach the end.
Re: QSqlQuery::isValid returns false
but i have the program that deal with access database which is built using visual basic
and this program go through the database easy it make last , first, next and previous
and i want to rebuild this program using c++. it means that the database support non sequential access
as well as sequential.