From e32074eef6afbe426355f43c7f4fa60e8e7c6116 Mon Sep 17 00:00:00 2001 From: hongyang <3160878898@qq.com> Date: Thu, 8 Jun 2023 12:56:15 +0800 Subject: [PATCH] hongyang's code --- src/sqlite.cpp | 328 +++++++++++++++++++++++++++++++++++++++++++++++++ src/sqlite.h | 143 +++++++++++++++++++++ 2 files changed, 471 insertions(+) create mode 100644 src/sqlite.cpp create mode 100644 src/sqlite.h diff --git a/src/sqlite.cpp b/src/sqlite.cpp new file mode 100644 index 00000000..03bb7d0f --- /dev/null +++ b/src/sqlite.cpp @@ -0,0 +1,328 @@ +#include +#include +#include +#include +#include +#include +#include +#include "../../include/tool/sqlite.h" + +namespace database +{ + sqlite::sqlite() + { + createDB(); + } + + sqlite::~sqlite() + { + db.close(); + } + + int sqlite::createDB() + { + int result; + if(QSqlDatabase::contains("QSQLITE")) + { + db = QSqlDatabase::database("QSQLITE"); + result = 0; + if(db.open() == false){ + qDebug() << "Error: Failed to connect database." << db.lastError(); + result = 2; + } + } + else + { + db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName("JointSecure.db"); + if(db.open() == false){ + qDebug() << "Error: Failed to connect database." << db.lastError(); + result = 2; + } + else + { + createMissionTable(); + createRobortTable(); + } + + result = 1; + } + + return result; + } + + int sqlite::createMissionTable() + { + int result; + QString createSql = + QString("CREATE TABLE Mission(\ + id INT PRIMARY KEY NOT NULL,\ + name TEXT NOT NULL,\ + type INT NOT NULL)"); + QSqlQuery query; + if(!query.exec(createSql)) + { + qDebug() << "Error: Fail to create table." << query.lastError(); + result = 0; + } + else + { + qDebug() << "Table created!"; + result = 1; + } + return result; + } + + int sqlite::createRobortTable() + { + int result; + QString createSql = + QString("CREATE TABLE Robort(\ + id INT PRIMARY KEY NOT NULL,\ + name TEXT NOT NULL,\ + type INT NOT NULL,\ + ip TEXT NOT NULL,\ + mission TEXT NOT NULL)"); + QSqlQuery query; + if(!query.exec(createSql)) + { + qDebug() << "Error: Fail to create table." << query.lastError(); + result = 0; + } + else + { + qDebug() << "Table created!"; + result = 1; + } + return result; + + } + + int sqlite::addMission(std::string name, int type) + { + //获得Mission表最大id + QSqlQuery id_query; + QString select_max_sql = QString("select max(id) from Mission"); + int max_id = 0; + id_query.prepare(select_max_sql); + if (!id_query.exec()) + { + qDebug() << "max_id error:"< sqlite::traverseMission() + { + std::vector missionlist; + QSqlQuery mission_query; + QString select_max_sql = QString("select name from Mission"); + int max_id = 0; + mission_query.prepare(select_max_sql); + if (!mission_query.exec()) + { + qDebug() << "Mission name error:"< +#include +#include +#include +#include +#include +#include + +namespace database +{ + +class sqlite +{ +public: + + sqlite(); + + ~sqlite(); + + QSqlDatabase db;//建立和qt和数据库连接 + QSqlQueryModel model;//保存和遍历select结果 + + int createDB(); + + int createMissionTable(); + + int createRobortTable(); + + int addMission(std::string name, int type); + + std::vector queryMissionROBORT(std::string name) + { + std::vector RobortList; + QString select_sql = "select name, mission from Robort"; + QSqlQuery query; + if(!query.exec(select_sql)) + { + qDebug()< traverseMission(); + + void queryMission(std::string name, int &id, int &type, std::vector &robort) + { + QString select_sql = "select id, name, type from Mission"; + QSqlQuery query; + if(!query.exec(select_sql)) + { + qDebug()<