databaseeventthread.cpp 1.4 KB

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