Hi,
I wrote simple code to see how it works, and.. ups.
Code:
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());
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());
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());
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:
qDebug() << query.lastError().text();
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?
Bookmarks