#ifndef MYSQL_H #define MYSQL_H //SQL #include #include #include #include #include #include #include #include #include /** * 取数据库文件(fileName 如 "config.db")的绝对路径。 * * 【为什么必须有这个函数】 * 之前用 QStandardPaths::GenericDataLocation 拼路径。本机(Android 10 / A133P)上 * GenericDataLocation 返回的是 /storage/emulated/0 (外置存储根目录), * 于是库落在 /storage/emulated/0/config.db。把 SQLite 库放在模拟存储上不可靠: * - 该路径可能因挂载状态 / 权限策略变化而不可用,open 直接失败,报 * "unable to open database file"(SQLITE_CANTOPEN); * - 模拟存储走 FUSE,对 SQLite 依赖的文件锁支持不佳,易出 I/O 类错误。 * * 因此改为优先落到应用私有目录:一定可写、无需权限、不受外置存储挂载影响, * 且是正常的 POSIX 文件系统(SQLite 锁没问题): * Android: AppLocalDataLocation / AppDataLocation (如 /data/data//files) * 其它平台: applicationDirPath * 同时做两件保护: * 1) 若私有目录已有同名库,直接复用; * 2) 若需要在私有目录新建,而旧位置(GenericDataLocation / 当前目录)存在可读的库, * 则先拷贝过来,避免 MQTT / FTP / 相机等配置回落到默认值。 * * GenericDataLocation 只会作为"旧库迁移来源",不会被选作目标 —— * 因为它的目录权限检查可能返回可写、实际仍会失败。 * * 结果按文件名缓存,进程内只解析一次。 */ QString appDbAbsolutePath(const QString &fileName); typedef struct { int id; QString dtype; QString ip; int port; QString imei; QString uid; QString pwd; QString clientid; int qos; int keepalive; QString sub; QString pub; QString offline; int dat_f; }config_mqtt_db; typedef struct { int id; QString ip; int port; QString dir; QString uid; QString pwd; int mode; }config_ftp_db; typedef struct { int id; QString lbs_lat; QString lbs_lng; QString gps_lat; QString gps_lng; QString gps_elevation; }config_location_db; typedef struct { int id; QString datetime; QString Json_data; QString Json_warn; }data_json_db; typedef struct{ QString camera_type; QString mode; int stitch_photonum; int photo_width; int photo_height; QString auto_focus; QString movz_f; QString movz_b; QString movx_f; QString movx_b; }config_camera; typedef struct{ int jsonCacheEnable; int imageCacheEnable;; int maxJsonEntries; int maxImageEntries; int x_init; int y_init; int fusion_enable; //景深融合开关 1=开 0=关(MQTT fusion_sw 下发) int focus_algo; //对焦/融合算法 0=SLView GFF 1=SLView MS 2=旧算法(MQTT focus_algo 下发) }config_param; class config_db_type { public: config_mqtt_db mqtt_db; config_ftp_db ftp_db; config_location_db location_db; data_json_db json_db; config_camera camera_db; config_param config_db; }; //int mode; //QString lbs_lat; //QString lbs_lng; //QString gps_lat; //QString gps_lng; //QString gps_elevation; //QJsonArray param::mqtt_array; class SqliteOperator : public QObject { Q_OBJECT public: SqliteOperator(QString &dbname); //创建数据库 //创建数据库 bool OpenDb(); //打开数据库 bool isTableExist(QString& tableName); //判断数据表是否存在 int CloseDb(); //关闭数据库 int CreateTable(QString& tableName); //创建数据表 int InsertData(config_db_type &singledb,QString tablename); //插入数据 void InertListConfig(QList &moreData,QString tablename); int UpdateData(QString tablename, QString name, QString value); //更新、修改数据 int QueryAllData(QString& table_name); //遍历查询 int QueryData(); //条件查询 void deleteData(int id); //删除数据 void deleteTable(QString& tableName); //删除数据表 private: QSqlDatabase db; // 用于建立和数据库的连接 }; bool InsertJson_mqtt(); //旧版本数据库缺少新加的列时补列(平滑升级用):已存在则直接返回true //columnDef: 形如 "fusion_enable INT NOT NULL DEFAULT 1" bool ensureColumnExist(const QString &table, const QString &column, const QString &columnDef); //确保 config 表至少有一行默认配置(表被清空时兜底补行) bool ensureConfigRow(); //条件查询与遍历查询的对比 //加循环建了一个包含229300条数据,7448kb的数据库。 //条件查询耗时:30ms //遍历查询耗时:100ms //随着数据量的增加,差距应该会更明显。 //qml接口 class DatabaseInterface : public QObject { Q_OBJECT public: explicit DatabaseInterface(QObject *parent = nullptr); Q_INVOKABLE bool openDatabase(const QString& dbName); Q_INVOKABLE bool closeDatabase(); Q_INVOKABLE QString getTable_row(QString tablename); Q_INVOKABLE bool createTable(QString tableName); Q_INVOKABLE bool insertData(const QString& name, const QString& address); Q_INVOKABLE bool deleteData(int id); Q_INVOKABLE bool updateData(QString tablename,QString name,QVariant value); Q_INVOKABLE QJsonArray queryData(QString table_name,int count_index); // ================= 数据库管理界面(表结构/数据浏览编辑)接口 ================= // 说明: 这些接口使用独立的命名连接, 只服务"数据库管理"界面 —— 打开 data.db // 不会影响 MQTT/FTP 写库使用的默认连接, 关闭界面也不会把业务连接一起关掉 // (旧实现用 addDatabase() 抢默认连接名, 会把业务连接顶掉, 隐患很大)。 // 所有表名/列名先做标识符校验再拼进 SQL, 数据值一律走绑定参数。 Q_INVOKABLE QString currentDatabase() const; // 当前打开的库(文件名) Q_INVOKABLE QString lastError() const; // 最近一次失败原因(可直接提示给用户) Q_INVOKABLE QStringList tableList(); // 当前库中的全部数据表 Q_INVOKABLE QJsonArray tableInfo(const QString &table); // 列结构: name/type/notnull/dflt/pk Q_INVOKABLE QString tableSql(const QString &table); // 建表语句(用于查看表结构) Q_INVOKABLE int tableRowCount(const QString &table); // 表总行数 // 分页 + 关键字过滤查询, 每行附带 __rowid(用于精确定位修改/删除) Q_INVOKABLE QJsonArray queryRows(const QString &table, int limit, int offset, const QString &keyword); Q_INVOKABLE bool updateCell(const QString &table, const QString &column, const QVariant &value, qint64 rowId); Q_INVOKABLE bool insertRow(const QString &table, const QVariantMap &values); Q_INVOKABLE bool deleteRow(const QString &table, qint64 rowId); Q_INVOKABLE bool addColumn(const QString &table, const QString &column, const QString &type, const QString &defaultValue); Q_INVOKABLE bool renameColumn(const QString &table, const QString &column, const QString &newName); Q_INVOKABLE bool dropColumn(const QString &table, const QString &column); //确保配置库(config.db)的默认连接已建立并打开(幂等)。 //启动流程/串口参数同步/GPS 等都会在结束时 CloseDb()(removeDatabase), //连接被移除后写库会直接失败(MQTT 下发配置响应 "db save failed") bool ensureConfigDbOpen(); private: // 惰性绑定到启动时 SqliteOperator 打开的 config.db 默认连接(qt_sql_default_connection) QSqlDatabase ensureDb(); // 标识符(表名/列名)白名单校验: 仅字母/数字/下划线, 且不以数字开头 bool checkIdentifier(const QString &name); QSqlDatabase m_database; QSqlDatabase m_mgrDb; // 管理界面专用连接(qt_sql_dbmanager_connection) QString m_currentDbName; // 管理界面当前打开的库文件名 QString m_lastError; // 最近一次失败原因 }; #endif // MYSQL_H