Advertisement
Guest User

Untitled

a guest
Jul 25th, 2012
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include "sqlworker.h"
  2. #include <QDebug>
  3. #include <QSqlError>
  4. #include <QSqlQuery>
  5. #include <QtSql>
  6.  
  7. sqlWorker::sqlWorker(const QString& hostName, const QString& dbName, const QString& userName, const QString& pwd)
  8. {
  9.     workDB = QSqlDatabase::addDatabase("QMYSQL", CONNECTION_NAME);
  10.     workDB.setHostName(hostName);
  11.     workDB.setDatabaseName(dbName);
  12.     workDB.setUserName(userName);
  13.     workDB.setPassword(pwd);
  14.     if(!workDB.open()) {
  15.         qDebug() << "Failed to open mysql database at host " << hostName << " with database name " << dbName << "and username " << userName;
  16.         qDebug() << "Reason : " << workDB.lastError().text();
  17.     } else {
  18.         qDebug("db opened successfull.");
  19.         query = new QSqlQuery(workDB);
  20.         qDebug("query created.");
  21.     }
  22. }
  23.  
  24. sqlWorker::sqlWorker()
  25. {
  26.     sqlWorker(DEFAULT_HOSTNAME, DEFAULT_DBNAME, DEFAULT_USERNAME, DEFAULT_PASSWORD);
  27. }
  28.  
  29. sqlWorker::~sqlWorker()
  30. {
  31.     qDebug() << "Entering sqlWorker::~sqlWorker() ...";
  32.     if(workDB.isOpen())
  33.         workDB.close();
  34.     QSqlDatabase::removeDatabase(CONNECTION_NAME);
  35. }
  36.  
  37. QString sqlWorker::sqlVersion()
  38. {
  39.  
  40.     qDebug() << "workDB status : " << (workDB.isOpen() ? "opened" : "CLOSED")/* << "query status : " << (query->isActive() ? "active" : "inactive")*/;
  41.     qDebug("Entering sqlVersion(). Sending query->exec()");
  42.     query->exec();
  43.     qDebug("Getting query->next()");
  44.     QString result;
  45.     while(query->next()) {
  46.         result.append("\t" + query->value(0).toString());
  47.     }
  48.     return result;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement