databaseeventthread.cpp 1.5 KB

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