parent
7e4bf8d123
commit
6a455c09c5
@ -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…
Reference in new issue