PDA

View Full Version : MySQL connects but doesn't queries, it queries if I do...



Raccoon29
16th November 2009, 18:56
Hi all,

I never saw this behavior before, so I'm a well bit confused, maybe I'm missing something stupid.
I built a minimal application just to test QMYSQL driver to use it in another project; the problem is that it connects, but queries are executed or not depending on how I do it:

-QSqlQuery::exec(QString query); -> works
-QSqlQuery::prepare(QString query); QSqlQuery::exec(); -> doesn't

no errors from the db, it just fails.
Someone knows why whould happen something like that?

This is the code I use:


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("dbname");
db.setUserName("username");
db.setPassword("password");
bool ok = db.open();
if(ok)
{
QMessageBox::information(this,"Connected","Database is connected");
QSqlQuery query(db);
query.prepare("SELECT * FROM categories");
if(!query.exec()) // instruction: query.exec("SELECT * FROM categories") would work
QMessageBox::critical(this,"Query error",db.lastError().text()); //no errors... but fails
db.close();
}
else
QMessageBox::critical(this,"Connect error",db.lastError().text());

By the way, categories table exists in that db.

Anybody? Thanks in advance

wysota
16th November 2009, 22:59
What does prepare() return?

Raccoon29
17th November 2009, 09:39
What does prepare() return?

I tested it as you suggested and it returned "true".

JD2000
17th November 2009, 12:55
QSqlQuery::exec() returns true if the query executed successfully

Last error is reset when exec is called. Hence no error message.

Raccoon29
17th November 2009, 14:26
QSqlQuery::exec() returns true if the query executed successfully

Last error is reset when exec is called. Hence no error message.
That's true, in fact exec() returns "false" and QSqlError()::text() is empty: that's the point.

Does the above code work for you (MySql as client, no embedded) ?

JD2000
17th November 2009, 16:44
I have just tried your code, both successfully and unsuccessfully on purpose by querying a non-existant table, it worked fine.

In the unsuccessful case as exec returns false (the query failed), the 'if' statement in line 12 is true and a QmessageBox appear containing the text "Query error".

There will be no error message from 'db' as 'lastError' had been reset by the exec call.
Does this help at all?

Raccoon29
17th November 2009, 18:58
I have just tried your code, both successfully and unsuccessfully on purpose by querying a non-existant table, it worked fine.
It worked for me too: correct query prepare() returns true, wrong query prepare() returns false. Until here everything seems right.


In the unsuccessful case as exec returns false (the query failed), the 'if' statement in line 12 is true and a QmessageBox appear containing the text "Query error".
Here comes the strange: in both cases of correct and incorrect query exec() returns false, without explanations, just an empty string.


There will be no error message from 'db' as 'lastError' had been reset by the exec call.
I hope that when exec() fails, lastError keeps its content, otherwise lastError itself has no reasons to exist...
Anyway to clear it out, I've tried the QMessageBox even before the exec() too, but same result: no error string. Furthermore when you get "Query error" I get nothing... The same code works for you, so maybe the problem is mine... maybe the driver? How to test it...?


Does this help at all?
Of course pal, every even such little idea is of help here :) (thank you so far, by the way)

Raccoon29
18th November 2009, 17:21
Ok men, I noticed a "typo" that suggests much things: lastError() is empty because I'm asking it to the wrong object, the error is in query (QSqlQuery), not in db (QSqlDatabase).
I missed that, ...but you too after all! :p

Now QSqlQuery::lastError() outputs:

Using unsupported buffer type: 253 (parameter: 3) QMYSQL3: Unable to bind outvalues
I'm going to ask it to San Google since I never saw this error before, then I'll post my solution here.

wysota
18th November 2009, 18:38
You probably have a datatype in your table that Qt can't understand. Or it's not a real query that you posted and you are actually trying to bind some values before doing exec.

Raccoon29
19th November 2009, 10:18
Thank you S.Wysota for your try, but table was just some varchar and integer, nothing dangerous.
Instead it comes out that the problem was the driver indeed, as we deducted from present brain storming: now works and I have just built again qsqlmysql4.dll following step-by-step the instructions on this site http://www.jiggerjuice.net/software/qt-sql-drivers.html and *replaced* those in qt/plugins/sqldrivers dir.
I emphatize "replaced" because if previous dll stays in plugin directory, even renamed as "ex qsqlmysql4.dll", the problems shows up again. Actually I don't know the exact motivation, anyway this reason isn't so bad...

Now anyway it works! (Apache-style)
Thank you all for your ever god-blessing help!
See you next mission ;)