Results 1 to 10 of 10

Thread: MySQL connects but doesn't queries, it queries if I do...

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default MySQL connects but doesn't queries, it queries if I do...

    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:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    2. db.setHostName("localhost");
    3. db.setDatabaseName("dbname");
    4. db.setUserName("username");
    5. db.setPassword("password");
    6. bool ok = db.open();
    7. if(ok)
    8. {
    9. QMessageBox::information(this,"Connected","Database is connected");
    10. QSqlQuery query(db);
    11. query.prepare("SELECT * FROM categories");
    12. if(!query.exec()) // instruction: query.exec("SELECT * FROM categories") would work
    13. QMessageBox::critical(this,"Query error",db.lastError().text()); //no errors... but fails
    14. db.close();
    15. }
    16. else
    17. QMessageBox::critical(this,"Connect error",db.lastError().text());
    To copy to clipboard, switch view to plain text mode 
    By the way, categories table exists in that db.

    Anybody? Thanks in advance
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MySQL connects but doesn't queries, it queries if I do...

    What does prepare() return?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: MySQL connects but doesn't queries, it queries if I do...

    Quote Originally Posted by S. wysota View Post
    What does prepare() return?
    I tested it as you suggested and it returned "true".
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  4. #4
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MySQL connects but doesn't queries, it queries if I do...

    QSqlQuery::exec() returns true if the query executed successfully

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

  5. #5
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: MySQL connects but doesn't queries, it queries if I do...

    Quote Originally Posted by JD2000 View Post
    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) ?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  6. #6
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MySQL connects but doesn't queries, it queries if I do...

    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?
    Last edited by JD2000; 17th November 2009 at 17:00.

  7. The following user says thank you to JD2000 for this useful post:

    Raccoon29 (19th November 2009)

  8. #7
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: MySQL connects but doesn't queries, it queries if I do...

    Quote Originally Posted by JD2000 View Post
    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.

    Quote Originally Posted by JD2000 View Post
    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.

    Quote Originally Posted by JD2000 View Post
    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...?

    Quote Originally Posted by JD2000 View Post
    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

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  9. #8
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default I am a dummy

    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!

    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.
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: I am a dummy

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Indeed it was the driver

    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
    Last edited by Raccoon29; 19th November 2009 at 10:32.
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.