PDA

View Full Version : QSqlQuery problem with SELECT statement



virtuosite
30th August 2009, 22:25
Hi,

I wrote simple code to see how it works, and.. ups.
Code:


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName("testing");
db.setUserName("test");
db.setPassword("test");

bool ok = db.open();
if(!ok)
QMessageBox::critical(this, "Cannot connect with DB", db.lastError().text());
QSqlQuery query(db);
query.exec(ui->query->text());
query.next();

ui->size->setText(QString::number(query.size()));
qDebug() << "Activity " << query.isActive();
qDebug() << "Select: " << query.isSelect();
qDebug() << "Ok " << query.isValid();
ui->result->setText(query.value(0).toString());


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:

qDebug() << query.lastError().text();
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?

victor.fernandez
31st August 2009, 07:55
Which version of Qt are you using? There was a bug (http://qt.nokia.com/developer/task-tracker/index_html?method=entry&id=203468) 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;"?

virtuosite
31st August 2009, 08:13
I have Qt 4.5.2 installed according to: http://christopher.rasch-olsen.no/2009/04/14/qt-45-and-mysql-plugin-with-mingw-on-windows-xp/

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