hongyang's code

dingzijian_develop
hongyang 2 years ago
parent 78acd60ced
commit e32074eef6

@ -0,0 +1,328 @@
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlQueryModel>
#include <QDebug>
#include <string>
#include <vector>
#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:"<<id_query.lastError();
}
else
{
while (id_query.next())
{
max_id = id_query.value(0).toInt();
qDebug()<<"max_id:"<<max_id;
}
}
//插入Mission
int key;
QString insertSql = "insert into Mission values (?, ?, ?)";
QSqlQuery query;
QVariant NAME;
NAME = QString::fromStdString(name);
query.prepare(insertSql);
query.addBindValue(max_id+1);
query.addBindValue(NAME);
query.addBindValue(type);
if(!query.exec())
{
qDebug() << "inserted failed: "<< query.lastError();
key=-1;
}
else
{
qDebug() << "inserted succeed";
}
return key;
}
int sqlite::addRobort(std::string name, int type, std::string ip, std::string mission )
{
QVariant NAME;
NAME = QString::fromStdString(name);
QVariant IP;
IP = QString::fromStdString(ip);
QVariant MISSION;
MISSION = QString::fromStdString(mission);
//获得Robort表最大id
QSqlQuery id_query;
QString select_max_sql = QString("select max(id) from Robort");
int max_id = 0;
id_query.prepare(select_max_sql);
if (!id_query.exec())
{
qDebug() << id_query.lastError();
}
else
{
while (id_query.next())
{
max_id = id_query.value(0).toInt();
qDebug()<<max_id;
}
}
//更新Robort表
QSqlQuery insert_query;
QString insertSql = "insert into Robort values (?, ?, ?, ?, ?)";
insert_query.prepare(insertSql);
insert_query.addBindValue(max_id+1);
insert_query.addBindValue(NAME);
insert_query.addBindValue(type);
insert_query.addBindValue(IP);
insert_query.addBindValue(MISSION);
if(!insert_query.exec())
{
qDebug() << insert_query.lastError();
}
else
{
qDebug() << "inserted succeed";
}
return 0;
}
int sqlite::modifyMission()
{
return 0;
}
int sqlite::modifyRobort()
{
return 0;
}
int sqlite::deleteMission()
{
return 0;
}
int sqlite::deleteRobort(std::string name, int type, std::string ip, std::string mission )
{
QString NAME;
NAME = QString::fromStdString(name);
QVariant IP;
IP = QString::fromStdString(ip);
QVariant MISSION;
MISSION = QString::fromStdString(mission);
//更新Mission表
QSqlQuery updata_query;
QString updateSql = QString("delete from Robort where name='%1'").arg(NAME);
qDebug()<<updateSql;
updata_query.prepare(updateSql);
if(!updata_query.exec())
{
qDebug() << updata_query.lastError();
}
else
{
qDebug() << "delete!!";
}
}
std::vector<std::string> sqlite::traverseMission()
{
std::vector<std::string> 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:"<<mission_query.lastError();
}
else
{
while (mission_query.next())
{
QString NAME = mission_query.value(0).toString();
std::string name = NAME.toStdString();
missionlist.push_back(name);
qDebug() << NAME;
}
}
return missionlist;
}
int queryMissionID(std::string name)
{
QString select_sql = "select id, name from Mission";
QSqlQuery query;
if(!query.exec(select_sql))
{
qDebug()<<query.lastError();
}
else
{
while(query.next())
{
int ID = query.value(0).toInt();
QString NAME = query.value(1).toString();
if(name == NAME.toStdString())
{
return ID;
}
//qDebug()<<QString("id:%1 name:%2").arg(id).arg(name);
}
qDebug()<<QString("not found name");
}
return 0;
}
int queryMissionTYPE(std::string name)
{
QString select_sql = "select name, type from Mission";
QSqlQuery query;
if(!query.exec(select_sql))
{
qDebug()<<query.lastError();
}
else
{
while(query.next())
{
QString NAME = query.value(0).toString();
int TYPE = query.value(1).toInt();
if(name == NAME.toStdString())
{
return TYPE;
}
//qDebug()<<QString("id:%1 name:%2").arg(id).arg(name);
}
qDebug()<<QString("not found name");
}
return 0;
}
int test = 1;
}

@ -0,0 +1,143 @@
#ifndef SQLITE_H
#define SQLITE_H
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlQueryModel>
#include <QDebug>
#include <vector>
#include <string>
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<std::string> queryMissionROBORT(std::string name)
{
std::vector<std::string> RobortList;
QString select_sql = "select name, mission from Robort";
QSqlQuery query;
if(!query.exec(select_sql))
{
qDebug()<<query.lastError();
}
else
{
while(query.next())
{
QString NAME = query.value(0).toString();
QString MISSION = query.value(1).toString();
if(name == MISSION.toStdString())
{
RobortList.push_back(NAME.toStdString());
}
//qDebug()<<QString("id:%1 name:%2").arg(id).arg(name);
}
qDebug()<<QString("not found name");
}
return RobortList;
}
int addRobort(std::string name, int type, std::string ip, std::string mission);
int modifyMission();
int modifyRobort();
int deleteMission();
int deleteRobort(std::string name, int type, std::string ip, std::string mission );
std::vector<std::string> traverseMission();
void queryMission(std::string name, int &id, int &type, std::vector<std::string> &robort)
{
QString select_sql = "select id, name, type from Mission";
QSqlQuery query;
if(!query.exec(select_sql))
{
qDebug()<<query.lastError();
}
else
{
while(query.next())
{
int ID = query.value(0).toInt();
QString NAME = query.value(1).toString();
int TYPE = query.value(2).toInt();
if(name == NAME.toStdString())
{
id = ID;
type = TYPE;
robort = queryMissionROBORT(name);
return;
}
//qDebug()<<QString("id:%1 name:%2").arg(id).arg(name);
}
qDebug()<<QString("not found id:%1").arg(id);
}
}
void queryRobort(std::string name, int &id, int &type, std::string &ip)
{
QString select_sql = "select id, name, type, ip from Robort";
QSqlQuery query;
if(!query.exec(select_sql))
{
qDebug()<<query.lastError();
}
else
{
while(query.next())
{
int ID = query.value(0).toInt();
QString NAME = query.value(1).toString();
int TYPE = query.value(2).toInt();
QString IP = query.value(3).toString();
if(name == NAME.toStdString())
{
id = ID;
type = TYPE;
ip = IP.toStdString();
return;
}
//qDebug()<<QString("id:%1 name:%2").arg(id).arg(name);
}
qDebug()<<QString("not found id:%1").arg(id);
}
}
int test;
};
}
#endif
Loading…
Cancel
Save