#include <QtGui>
#include <QtSql>
#include <QApplication>
class ABC {
public:
QSqlDatabase db;
QSqlQuery memberQuery;
ABC() {
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test");
qDebug() << "Database open test : " << db.open();
}
void database() {
qDebug() << "Inside database method..." << endl;
QSqlQuery localQuery ;
qDebug() << "Using local Query : " << localQuery.exec("create table if not exists alu (ini int)");
localQuery.clear();
qDebug() << "Using memeber query object: " << memberQuery.exec("create table if not exists alu (ini int)");
memberQuery.clear();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ABC ob;
ob.database();
return a.exec();
}
执行此代码后发现以下结果。
Database open test : true
Inside database method...
Using local Query : true
QSqlQuery::exec: database not open
Using memeber query object: false
我的问题是为什么我不能使用成员对象 QSqlQuery 而不是本地对象?