public class MySqlDB{public: MySqlDB(QString fileName); bool createConnection(); void close(); void f1(); void f2();private: QString CONNECTION_NAME; QString dbFile; bool createAndExecuteQuery(QString str);}MySqlDB::MySqlDB(QString file){.... dbFile = file; CONNECTION_NAME = QString("conn");}bool MySqlDB::createConnection(){ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME) db.setDatabaseName(dbFile); db.setUserName("user"); db.setHostName("host"); db.setPassword("pass"); if(!db.open()) { return false; } return true;}bool MySqlDB::createAndExecuteQuery(QString str){ QSqlDatabase db = QSqlDatabase::database(CONNECTION_NAME); QSqlQuery query(db); if(!query.exec(str)) { return false; } return true;}void MySqlDB::close(){ { QSqlDatabase db = QSqlDatabase::database(CONNECTION_NAME); db.close(); } QSqlDatabase::removeDatabase(CONNECTION_NAME)}
C++ (Qt) if(!query.exec(str)) { return false; } return true;
C++ (Qt) return query.exec(str);