PDA

View Full Version : QSqlQuery::lastquery() returns blank



Qt Coder
19th September 2009, 07:50
I hav configued the Mysql successfully, now its showing QMYSQL in availabel drivers list.

I configured Mysql Server5.0 on my local machine and created database.

Using below code ,it connects to database but when I query it ,doesnt return any records,
so I tried checking my query with Query.lastquery() just before I execute it,and found that its returning blank.that means the query is not set properly ,what could be the error?




QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
QSqlQuery query;
bool ok;

db.setHostName("localhost");
db.setDatabaseName("ams");
db.setUserName("root");
db.setPassword("gauranga");
db.setPort(3306);

if(!db.isOpen())
ok = db.open();

if(!ok)
{
out << "\nError Driver text: " << db.lastError().driverText() ;
out << "\nError Database text :" << db.lastError().databaseText();

}
else
{
out <<"DB open successfully";
ok = query.prepare("SELECT * FROM info");
out << "\nQuery is: " << query.lastQuery();
}

Qt Coder
19th September 2009, 08:27
If I supply query while creating qsqlquery object then it runs perfectly well and returns resultset.

QSqlQuery query("SELECT * FROM temp",db);


but if I use

ok = query.prepare("SELECT * FROM temp");

then it does not set the query and returns empty string as a result of query.lastQuery()


strange behaviour..