PDA

View Full Version : QSqlQuery problem



MarkoSan
14th January 2008, 18:46
Hi to all!

Here is the code:
QString queryString("SELECT * from merchandize WHERE GrupaIdentificationNumber=%1;");
//QString queryString("SELECT * from merchandize WHERE GrupaIdentificationNumber=2;"); // test
queryString=queryString.arg(groupId);
qDebug() << "Query: " << queryString; // debug

QSqlQuery query(queryString); // sets up query from query string
removeAllMerchandize(); // removes all items
m_iImageCounter=0; // inits counter
//query.exec(queryString); // executes query string
while (query.next())
{
.....
}

As you can see, I've debugged created query and it is ok. But why the program flow skips while loop. The database is filled and the application is connected to it as it must be. I thought there is something wrong with query itself, but as I copied quey, which was produces with qDebug() and pasted it into mysql by hand, I got right results. Can someone help me please?

jacek
14th January 2008, 18:49
What does QSqlQuery::isActive() return just before the loop?

MarkoSan
14th January 2008, 18:56
God damn, I've recoded the code using isActive like that:
void CMerchandizeBrowser::fillMerchandize(int groupId)
{
qDebug() << "Entered CMerchandizeBrowser::fillMerchandize ..."; // debug
// sets up query string
QString queryString("SELECT * from merchandize WHERE GrupaIdentificationNumber=%1;");
//QString queryString("SELECT * from merchandize WHERE GrupaIdentificationNumber=2;"); // test
queryString=queryString.arg(groupId);
qDebug() << "Query: " << queryString; // debug

QSqlQuery query(queryString); // sets up query from query string
removeAllMerchandize(); // removes all items
m_iImageCounter=0; // inits counter
//query.exec(queryString); // executes query string
// WARNING: Are we connected to database????
if (query.isActive())
{
while (query.next())
{
/*
if (groupId==query.value(iMerchandizeFieldGROUPID).to Int())
{
*/
QImage img(query.value(iMerchandizeFieldPICPATH).toString ()); // fetches image from filename

tEmbeddImagesResult result=embeddedPicPath(img, query.value(iMerchandizeFieldPICPATH).toString()); // embedd pic path into pic
if (result!=resFailue)
{
img=img.convertToFormat(QImage::Format_ARGB32_Prem ultiplied);
}
//addMerchandize(query.value(iMerchandizeFieldPICPAT H).toString()); // adds merchandize pics
setSlideCount(m_iImageCounter+1);
setSlide(m_iImageCounter, img);
m_iImageCounter++;
//}
} // while
};
m_iSelected=m_iImageCounter / 2; // sets selected pic

//update(); // updates window
qDebug() << "m_iImageCounter: " << m_iImageCounter; // debug
qDebug() << "m_iSelected: " << m_iSelected; // debug
qDebug() << "Exiting CMerchandizeBrowser::fillMerchandize ..."; // debug
}

and isActive under if skips while loop. Thas means I am not connected to database??

jacek
14th January 2008, 19:08
Thas means I am not connected to database??
Not necessarily. What does QSqlQuery::lastError() return?

MarkoSan
14th January 2008, 19:28
God damn, the line
qDebug() << query.lastError().text(); // debugreturns
warning: "No database selected QMYSQL: Unable to execute query" . Jacek, thanks for hints, but clear this up for me please. How is it possible that in the same object and in other method, one time the database is active and the second time, like in this problem, it goes offline. Is there any timeout setting for MySQL database???:confused:

jacek
14th January 2008, 19:44
What happens in removeAllMerchandize()?

MarkoSan
14th January 2008, 19:54
Here is code, nothing special:
void CMerchandizeBrowser::removeAllMerchandize()
{
qDebug() << "Entered CMerchandizeBrowser::removeAllMerchandize ..."; // debug
// qDebug() << "Number of pics: " << m_ImagesList.count();
// for(qint16 iIndex=0; iIndex<m_ImagesList.count(); iIndex++)
// {
// m_ImagesList.removeAt(iIndex); // removes item
// }
//clear(); // clear slides of all images
d->slideImages.clear(); // clears intertnal pic presentation
//update(); // updates screen
qDebug() << "Exiting CMerchandizeBrowser::removeAllMerchandize ..."; // debug
}

And d is private class under CMerchandizeBrowser. Let me comment it and then see what happens.

jacek
14th January 2008, 19:56
How do you connect to the database? Do you use a named connection?

MarkoSan
14th January 2008, 20:04
I've coded database singleton class with your help, I think, a while ago and I am reusing it now in this application. Do you need a code?

jacek
14th January 2008, 20:17
No, I just want to know the line with addDatabase().

MarkoSan
14th January 2008, 20:55
Ok, here is the whole constructor of my CDatabaseFoundation class:
CDatabaseFoundation::CDatabaseFoundation(QString db_type, QString db_host,
QString db_name, QString db_username,
QString db_password): QObject()
{
// sets up database paramaters
m_Database=QSqlDatabase::addDatabase(db_type);
m_Database.setHostName(db_host);
m_Database.setUserName(db_username);
m_Database.setPassword(db_password);

// tries to connect to database
m_bConnectionEstablished=m_Database.open();
if(!m_bConnectionEstablished)
qFatal("Failed to connect to database. Aborting.");
//create database + tables if they don't exists yet
//CreateDatabaseAndTables();

//call seletedDatabase statement here because you're sure the the database exists
m_Database.setDatabaseName(db_name);


/*
m_pModel=new QSqlRelationalTableModel(this);
Q_CHECK_PTR(m_pModel);
//all changes will be cached in the model until either submitAll() or revertAll() is called
m_pModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
*/
//addRelations(m_pModel);

}

jacek
14th January 2008, 20:58
// tries to connect to database
m_bConnectionEstablished=m_Database.open();
if(!m_bConnectionEstablished)
qFatal("Failed to connect to database. Aborting.");
//create database + tables if they don't exists yet
//CreateDatabaseAndTables();

//call seletedDatabase statement here because you're sure the the database exists
m_Database.setDatabaseName(db_name);

As the setDatabaseName() docs say: "This must be done before the connection is opened or it has no effect;". Put that line before the call to open().

MarkoSan
14th January 2008, 21:06
Ok, jacek, now query works fine. But please tell me, how is it possible that the SAME CODE worked before?????

jacek
14th January 2008, 21:31
how is it possible that the SAME CODE worked before?????
Yesterday was the 13th.

MarkoSan
14th January 2008, 22:50
Hmm, you can mock at me as much as you want, but in the same object other method works without problem, I tried it now. :D:rolleyes: I have to solve this mistery, anyway, jacek, thank you very very much for your guidance.