class DatabaseAccessor {public: static DatabaseAccessor* getInstance(); static QString dbHost; static QString dbName; static QString dbUser; static QString dbPass;public slots: void executeSqlQuery(QString); void validateUser(QString, QString);private: DatabaseAccessor(); DatabaseAccessor(const DatabaseAccessor& ); DatabaseAccessor& operator=(const DatabaseAccessor& ); QSqlDatabase db; };
class DatabaseAccessor:public QObject {Q_OBJECTpublic: static DatabaseAccessor* getInstance(); static QString dbHost; static QString dbName; static QString dbUser; static QString dbPass;private: DatabaseAccessor(); DatabaseAccessor(const DatabaseAccessor& ); DatabaseAccessor& operator=(const DatabaseAccessor& ); QSqlDatabase db; };
C++ (Qt)QString DatabaseAccessor::dbHost;QString DatabaseAccessor::dbName;...
DatabaseAccessor::DatabaseAccessor(QObject *parent) { DatabaseAccessor::dbHost="localhost"; db=QSqlDatabase::addDatabase("QMYSQL"); db.setHostName(dbHost); db.setDatabaseName(dbName); db.setUserName(dbUser); db.setPassword(dbPass); if (db.open()) { qDebug("connected to database"); } else { qDebug("Error occured in connection to database"); } }
int main(int argc, char **argv) { QCoreApplication app(argc, argv); DatabaseAccessor::dbHost="localhost"; DatabaseAccessor::getInstance();