Results 1 to 3 of 3

Thread: QSqlQuery problem with SELECT statement

  1. #1
    Join Date
    Aug 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QSqlQuery problem with SELECT statement

    Hi,

    I wrote simple code to see how it works, and.. ups.
    Code:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    2. db.setHostName("localhost");
    3. db.setPort(3306);
    4. db.setDatabaseName("testing");
    5. db.setUserName("test");
    6. db.setPassword("test");
    7.  
    8. bool ok = db.open();
    9. if(!ok)
    10. QMessageBox::critical(this, "Cannot connect with DB", db.lastError().text());
    11. QSqlQuery query(db);
    12. query.exec(ui->query->text());
    13. query.next();
    14.  
    15. ui->size->setText(QString::number(query.size()));
    16. qDebug() << "Activity " << query.isActive();
    17. qDebug() << "Select: " << query.isSelect();
    18. qDebug() << "Ok " << query.isValid();
    19. ui->result->setText(query.value(0).toString());
    To copy to clipboard, switch view to plain text mode 

    Table test in database testing have two columns, id and text, I inserted some data to it via phpmyadmin(~50 records). ui->query, ui->size and ui->result are QLineEdit Widgets.
    I see in console that Activity is true, the sam as Select, but "ok" is false. I se that result widget is empty, and message in console: "QSqlQuery::value: not positioned on a valid record". It`s for me wery strange, i see in size widget value "48", so data exist in database, but there is a problem with getting value. I tried to INSERT some data via query.exec() method, it works fine. I also tried add ";" after query strong, no results.

    What i`m doing wrong?

    ---
    After add this:
    Qt Code:
    1. qDebug() << query.lastError().text();
    To copy to clipboard, switch view to plain text mode 
    I see: " QMYSQL3: Unable to fetch data". Why QMYSQL3, not QMYSQL?
    I have avaliable drivers: "QSQLITE QMYSQL3 QMYSQL QODBC3 QODBC". Maybe I have to force loading driver QMYSQL? How can I do this?
    Last edited by virtuosite; 30th August 2009 at 23:41.

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem with SELECT statement

    Which version of Qt are you using? There was a bug in Qt 4.4.1 and earlier where you would get exactly that same error message when the result of the query is empty. Did you try with "SELECT 1;"?

  3. #3
    Join Date
    Aug 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem with SELECT statement

    I have Qt 4.5.2 installed according to: http://christopher.rasch-olsen.no/20...on-windows-xp/

    Yes, I also tried "SELECT 1;" (and "SELECT 1" without semicolon), I get the same messages.

Similar Threads

  1. SQL Select statement with binary comparison
    By xfurrier in forum Qt Programming
    Replies: 2
    Last Post: 21st June 2009, 03:33
  2. Replies: 9
    Last Post: 6th May 2009, 11:09
  3. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 10:12
  4. QSqlQuery problem
    By MarkoSan in forum Qt Programming
    Replies: 14
    Last Post: 14th January 2008, 23:50
  5. QSqlQuery problem
    By MarkoSan in forum Qt Programming
    Replies: 11
    Last Post: 18th December 2007, 14:25

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.