mysql.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #ifndef MYSQL_H
  2. #define MYSQL_H
  3. //SQL
  4. #include <QSqlDatabase>
  5. #include <QSqlError>
  6. #include <QSqlQuery>
  7. #include <main.h>
  8. #include <m_loger.h>
  9. /**
  10. * 取数据库文件(fileName 如 "config.db")的绝对路径。
  11. *
  12. * 【为什么必须有这个函数】
  13. * 之前用 QStandardPaths::GenericDataLocation 拼路径。本机(Android 10 / A133P)上
  14. * GenericDataLocation 返回的是 /storage/emulated/0 (外置存储根目录),
  15. * 于是库落在 /storage/emulated/0/config.db。把 SQLite 库放在模拟存储上不可靠:
  16. * - 该路径可能因挂载状态 / 权限策略变化而不可用,open 直接失败,报
  17. * "unable to open database file"(SQLITE_CANTOPEN);
  18. * - 模拟存储走 FUSE,对 SQLite 依赖的文件锁支持不佳,易出 I/O 类错误。
  19. *
  20. * 因此改为优先落到应用私有目录:一定可写、无需权限、不受外置存储挂载影响,
  21. * 且是正常的 POSIX 文件系统(SQLite 锁没问题):
  22. * Android: AppLocalDataLocation / AppDataLocation (如 /data/data/<pkg>/files)
  23. * 其它平台: applicationDirPath
  24. * 同时做两件保护:
  25. * 1) 若私有目录已有同名库,直接复用;
  26. * 2) 若需要在私有目录新建,而旧位置(GenericDataLocation / 当前目录)存在可读的库,
  27. * 则先拷贝过来,避免 MQTT / FTP / 相机等配置回落到默认值。
  28. *
  29. * GenericDataLocation 只会作为"旧库迁移来源",不会被选作目标 ——
  30. * 因为它的目录权限检查可能返回可写、实际仍会失败。
  31. *
  32. * 结果按文件名缓存,进程内只解析一次。
  33. */
  34. QString appDbAbsolutePath(const QString &fileName);
  35. typedef struct
  36. {
  37. int id;
  38. QString dtype;
  39. QString ip;
  40. int port;
  41. QString imei;
  42. QString uid;
  43. QString pwd;
  44. QString clientid;
  45. int qos;
  46. int keepalive;
  47. QString sub;
  48. QString pub;
  49. QString offline;
  50. int dat_f;
  51. }config_mqtt_db;
  52. typedef struct
  53. {
  54. int id;
  55. QString ip;
  56. int port;
  57. QString dir;
  58. QString uid;
  59. QString pwd;
  60. int mode;
  61. }config_ftp_db;
  62. typedef struct
  63. {
  64. int id;
  65. QString lbs_lat;
  66. QString lbs_lng;
  67. QString gps_lat;
  68. QString gps_lng;
  69. QString gps_elevation;
  70. }config_location_db;
  71. typedef struct
  72. {
  73. int id;
  74. QString datetime;
  75. QString Json_data;
  76. QString Json_warn;
  77. }data_json_db;
  78. typedef struct{
  79. QString camera_type;
  80. QString mode;
  81. int stitch_photonum;
  82. int photo_width;
  83. int photo_height;
  84. QString auto_focus;
  85. QString movz_f;
  86. QString movz_b;
  87. QString movx_f;
  88. QString movx_b;
  89. }config_camera;
  90. typedef struct{
  91. int jsonCacheEnable;
  92. int imageCacheEnable;;
  93. int maxJsonEntries;
  94. int maxImageEntries;
  95. int x_init;
  96. int y_init;
  97. int fusion_enable; //景深融合开关 1=开 0=关(MQTT fusion_sw 下发)
  98. int focus_algo; //对焦/融合算法 0=SLView GFF 1=SLView MS 2=旧算法(MQTT focus_algo 下发)
  99. }config_param;
  100. class config_db_type
  101. {
  102. public:
  103. config_mqtt_db mqtt_db;
  104. config_ftp_db ftp_db;
  105. config_location_db location_db;
  106. data_json_db json_db;
  107. config_camera camera_db;
  108. config_param config_db;
  109. };
  110. //int mode;
  111. //QString lbs_lat;
  112. //QString lbs_lng;
  113. //QString gps_lat;
  114. //QString gps_lng;
  115. //QString gps_elevation;
  116. //QJsonArray param::mqtt_array;
  117. class SqliteOperator : public QObject
  118. {
  119. Q_OBJECT
  120. public:
  121. SqliteOperator(QString &dbname); //创建数据库
  122. //创建数据库
  123. bool OpenDb(); //打开数据库
  124. bool isTableExist(QString& tableName); //判断数据表是否存在
  125. int CloseDb(); //关闭数据库
  126. int CreateTable(QString& tableName); //创建数据表
  127. int InsertData(config_db_type &singledb,QString tablename); //插入数据
  128. void InertListConfig(QList<config_db_type> &moreData,QString tablename);
  129. int UpdateData(QString tablename, QString name, QString value); //更新、修改数据
  130. int QueryAllData(QString& table_name); //遍历查询
  131. int QueryData(); //条件查询
  132. void deleteData(int id); //删除数据
  133. void deleteTable(QString& tableName); //删除数据表
  134. private:
  135. QSqlDatabase db; // 用于建立和数据库的连接
  136. };
  137. bool InsertJson_mqtt();
  138. //旧版本数据库缺少新加的列时补列(平滑升级用):已存在则直接返回true
  139. //columnDef: 形如 "fusion_enable INT NOT NULL DEFAULT 1"
  140. bool ensureColumnExist(const QString &table, const QString &column, const QString &columnDef);
  141. //确保 config 表至少有一行默认配置(表被清空时兜底补行)
  142. bool ensureConfigRow();
  143. //条件查询与遍历查询的对比
  144. //加循环建了一个包含229300条数据,7448kb的数据库。
  145. //条件查询耗时:30ms
  146. //遍历查询耗时:100ms
  147. //随着数据量的增加,差距应该会更明显。
  148. //qml接口
  149. class DatabaseInterface : public QObject
  150. {
  151. Q_OBJECT
  152. public:
  153. explicit DatabaseInterface(QObject *parent = nullptr);
  154. Q_INVOKABLE bool openDatabase(const QString& dbName);
  155. Q_INVOKABLE bool closeDatabase();
  156. Q_INVOKABLE QString getTable_row(QString tablename);
  157. Q_INVOKABLE bool createTable(QString tableName);
  158. Q_INVOKABLE bool insertData(const QString& name, const QString& address);
  159. Q_INVOKABLE bool deleteData(int id);
  160. Q_INVOKABLE bool updateData(QString tablename,QString name,QVariant value);
  161. Q_INVOKABLE QJsonArray queryData(QString table_name,int count_index);
  162. //确保配置库(config.db)的默认连接已建立并打开(幂等)。
  163. //启动流程/串口参数同步/GPS 等都会在结束时 CloseDb()(removeDatabase),
  164. //连接被移除后写库会直接失败(MQTT 下发配置响应 "db save failed")
  165. bool ensureConfigDbOpen();
  166. private:
  167. // 惰性绑定到启动时 SqliteOperator 打开的 config.db 默认连接(qt_sql_default_connection)
  168. QSqlDatabase ensureDb();
  169. QSqlDatabase m_database;
  170. };
  171. #endif // MYSQL_H