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:
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");
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());
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());
To copy to clipboard, switch view to plain text mode
By the way, categories table exists in that db.
Anybody? Thanks in advance
Bookmarks