/* Copyright(c)--Navinfo--Author:fangzurui--date:2023-10-20 */ #include "PositionDb.h" PositionDb::PositionDb() {} PositionDb::~PositionDb() {} bool PositionDb::open(QString dbPath) { _posDb = QSqlDatabase::addDatabase("QSQLITE"); _posDb.setDatabaseName(dbPath); if(_posDb.open() == false) { return false; } else { return true; } } void PositionDb::close() { if(_posDb.isOpen() == true) { _posDb.close(); } } MapPoint PositionDb::getPosition() { // 查询数据 QSqlQuery query; query = QSqlQuery(_posDb); // 搜索语句 query.prepare("SELECT * FROM position"); query.exec(); // 获取查询结果 QSqlRecord record = query.record(); if(record.isEmpty() == true) { return {0, 0, 0}; } else { // 创建一个地图点 MapPoint mapPoint; while(query.next()) { double lon, lat; lon = query.value("LONGITUDE").toDouble(); lat = query.value("LATITUDE").toDouble(); if(lon != 0.0) { mapPoint._dLon = lon; mapPoint._dLat = lat; mapPoint._dAlt = 2000; // 默认值设定为2000m } else { return {0, 0, 0}; } } return mapPoint; } return {0, 0, 0}; } void PositionDb::savePosition(MapPoint mapPoint) { // 查询数据 QSqlQuery query; query = QSqlQuery(_posDb); // 插入数据 query.prepare("INSERT into position(ID, CREATE_TIME, LONGITUDE, LATITUDE) values (:ID, :UPDATE_TIME, :LONGITUDE, :LATITUDE)"); query.bindValue(":ID", 1); // 获取当前时间 QDateTime dateTime = QDateTime::currentDateTime(); QString strDate = dateTime.toString("yyyy-MM-dd hh:mm:ss"); query.bindValue(":UPDATE_TIME", strDate); query.bindValue(":LONGITUDE", mapPoint._dLon); query.bindValue(":LATITUDE", mapPoint._dLat); query.exec(); } void PositionDb::clearPosition() { // 查询数据 QSqlQuery query; query = QSqlQuery(_posDb); // 删除词条 query.prepare("DELETE FROM position"); query.exec(); }