databaseeventthread.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "databaseeventthread.h"
  2. //#define HostName "172.17.35.51"
  3. //#define HostName "124.71.140.20"
  4. #define HostName "172.16.120.165"
  5. #define HostPort 3306
  6. #define UserName "usky"
  7. #define PassWord "Yt#75Usky"
  8. #define DatabaseName "usky-data"
  9. DatabaseEventThread::DatabaseEventThread(QObject *parent) : QThread(parent)
  10. {
  11. hour = 255;
  12. keep=false;
  13. sqlList.clear();
  14. db = QSqlDatabase::addDatabase("QMYSQL","write_db1");
  15. db.setHostName(QString(HostName));
  16. db.setPort(HostPort);
  17. db.setUserName(QString(UserName));
  18. db.setPassword(QString(PassWord));
  19. db.setDatabaseName(QString(DatabaseName));
  20. }
  21. void DatabaseEventThread::appendSql(QString sql)
  22. {
  23. sqlList.append(sql);
  24. }
  25. void DatabaseEventThread::stop()
  26. {
  27. keep = false;
  28. }
  29. void DatabaseEventThread::run()
  30. {
  31. keep = true;
  32. while (keep) {
  33. if(!db.open()){
  34. db.open();
  35. }else{
  36. if(sqlList.length()>0){
  37. while (sqlList.length()>0) {
  38. db.exec(sqlList.first());
  39. emit dbdata_log(QString("[%1] DatabaseEventThread sqlList.length() %2 writeProcess sql %3").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")).arg(sqlList.length()).arg(sqlList.first()));
  40. sqlList.removeFirst();
  41. usleep(1000);
  42. }
  43. }
  44. }
  45. usleep(50000);
  46. }
  47. }