databasethread.cpp 991 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "databasethread.h"
  2. DatabaseThread::DatabaseThread(QObject *parent, QSqlDatabase *db) :
  3. QThread(parent)
  4. {
  5. Db = db;
  6. hour = 255;
  7. keep=false;
  8. sqlList.clear();
  9. }
  10. void DatabaseThread::appendSql(QString sql)
  11. {
  12. sqlList.append(sql);
  13. }
  14. void DatabaseThread::stop()
  15. {
  16. keep = false;
  17. }
  18. void DatabaseThread::run()
  19. {
  20. keep = true;
  21. while (keep) {
  22. if(sqlList.length()>0)
  23. {
  24. if(Db->transaction())
  25. {
  26. while(sqlList.length()>0)
  27. {
  28. emit dbdata_log(QString("[%1] do sql: \"%2\"\r\n")
  29. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  30. .arg(sqlList.first()));
  31. Db->exec(sqlList.first());
  32. sqlList.removeFirst();
  33. usleep(10000);
  34. }
  35. Db->commit();
  36. }
  37. }
  38. usleep(50000);
  39. }
  40. }