1 Star 8 Fork 2

UNSTOPPABLE/ 毕设大作业基于qt5开发的局域网聊天软件

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sqlutil.cpp 5.17 KB
一键复制 编辑 原始数据 按行查看 历史
UNSTOPPABLE 提交于 2022-08-19 16:11 . 首次提交
#include "sqlutil.h"
#include <QDebug>
SqlUtil::SqlUtil(QObject *parent) : QObject(parent)
{
this->initSqlLite();
}
void SqlUtil::initSqlLite(){
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("talk.db");
QString s = db.connectionName();
psql = new QSqlQuery();
}
void SqlUtil::openDb(){
if(!db.open())
{
qDebug() << "sqllite打开失败";
}else{
qDebug() << "sqllite打开成功";
bool contactTsuccess = psql->exec("CREATE TABLE [Contact]([ContactId] INTEGER NOT NULL,[SourceHostUserName] NVARCHAR(40) NOT NULL,[SourceHostName] NVARCHAR(30) NOT NULL,[NickName] NVARCHAR(30),[MsgPayLoad] NVARCHAR(300),[SourceMac] NVARCHAR(80) NOT NULL,[TimeStamp1] NVARCHAR(70),[Version] NVARCHAR(10) NOT NULL,[PackageNo] NVARCHAR(10) NOT NULL,[MsgTypeCode] NVARCHAR(24) NOT NULL,[SourceIp] NVARCHAR(24),[AvatarPath] NVARCHAR(60),CONSTRAINT [PK_Customer] PRIMARY KEY ([ContactId]));");
bool TalkRecordTsuccess = psql->exec("CREATE TABLE [TalkRecord]([TalkRecordId] INTEGER NOT NULL,[ContactMac] NVARCHAR(80) NOT NULL,[SourceMac] NVARCHAR(80) NOT NULL,[MsgPayLoad] NVARCHAR(300),[TimeStamp1] NVARCHAR(70),[MsgTypeCode] NVARCHAR(24) NOT NULL,CONSTRAINT [PK_TalkRecord] PRIMARY KEY ([TalkRecordId]));");
emit initSuccess();
}
}
QSqlQuery* SqlUtil::selectDb(QString tableName)
{
QString sqlStr = "SELECT * from "+tableName;
if(psql->exec(sqlStr)){
return psql;
}
return nullptr;
}
void SqlUtil::insertDb(QString tableName,QList<QString> *bindKeyList,QList<QString> *bindValList){
QString paramsPlace="?";
for(int i=1;i<bindValList->size();i++){
paramsPlace+=",?";
}
QString sqlStr = "INSERT INTO "+tableName+"("+bindKeyList->join(",")+") VALUES ("+paramsPlace+")";
psql->prepare(sqlStr);
for(int i=0;i<bindValList->size();i++){
psql->addBindValue(bindValList->value(i));
}
bool success = psql->exec();
// qDebug() << success<<sqlStr;
}
void SqlUtil::addContact(ContactInfo *contactinfo)
{
QList<QString> bindKeyList={"SourceHostUserName","SourceHostName","NickName","MsgPayLoad","SourceMac","TimeStamp1","Version","PackageNo","MsgTypeCode","SourceIp","AvatarPath"};
QList<QString> bindValList={contactinfo->sourceHostUserName,contactinfo->sourceHostName,contactinfo->sourceHostUserName,contactinfo->msgPayLoad,contactinfo->sourceMac,contactinfo->timeStamp,contactinfo->version,contactinfo->packageNo,contactinfo->msgTypeCode,contactinfo->sourceIp,contactinfo->avatarPath};
insertDb("Contact",&bindKeyList,&bindValList);
}
QList<ContactInfo*>* SqlUtil::contactList()
{
QList<ContactInfo*> *contactList=new QList<ContactInfo*>;
QSqlQuery *query=selectDb("Contact");
while(query->next())
{
ContactInfo *contactInfo=new ContactInfo();
contactInfo->version=query->value("Version").toString();
contactInfo->packageNo=query->value("PackageNo").toString();
contactInfo->sourceMac=query->value("SourceMac").toString();
contactInfo->timeStamp=query->value("TimeStamp1").toString();
contactInfo->sourceHostUserName=query->value("SourceHostUserName").toString();
contactInfo->sourceHostName=query->value("SourceHostName").toString();
contactInfo->msgTypeCode=query->value("MsgTypeCode").toString();
contactInfo->msgPayLoad=query->value("MsgPayLoad").toString();
contactInfo->sourceIp=query->value("SourceIp").toString();
contactList->push_back(contactInfo);
}
return contactList;
}
bool SqlUtil::contactListByMacAddr(ContactInfo *contactinfo)
{
QString sqlStr = "SELECT * from Contact where SourceMac=?";
psql->prepare(sqlStr);
psql->addBindValue(contactinfo->sourceMac);
bool success = psql->exec();
if(psql->last()){
// qDebug() <<psql->at()+1;
return true;
}
psql->first();
return false;
}
QList<ContactInfo*>* SqlUtil::talkRecordListByContactMac(ContactInfo *contactinfo)
{
QString sqlStr = "SELECT * from TalkRecord WHERE ContactMac=?;";
psql->prepare(sqlStr);
psql->addBindValue(contactinfo->sourceMac);
psql->exec();
QList<ContactInfo*> *contactMsgList=new QList<ContactInfo*>;
while(psql->next())
{
ContactInfo *contactInfo=new ContactInfo();
contactInfo->version="";
contactInfo->packageNo="";
contactInfo->sourceMac=psql->value("SourceMac").toString();
contactInfo->timeStamp=psql->value("TimeStamp1").toString();
contactInfo->sourceHostUserName="";
contactInfo->sourceHostName="";
contactInfo->msgTypeCode=psql->value("MsgTypeCode").toString();
contactInfo->msgPayLoad=psql->value("MsgPayLoad").toString();
contactInfo->sourceIp="";
contactInfo->ContactMac=psql->value("ContactMac").toString();
contactMsgList->push_back(contactInfo);
}
return contactMsgList;
}
void SqlUtil::addMsg(ContactInfo *contactinfo)
{
QList<QString> bindKeyList={"ContactMac","SourceMac","MsgPayLoad","TimeStamp1","MsgTypeCode"};
QList<QString> bindValList={contactinfo->ContactMac,contactinfo->sourceMac,contactinfo->msgPayLoad,contactinfo->timeStamp,contactinfo->msgTypeCode};
insertDb("TalkRecord",&bindKeyList,&bindValList);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/UnStoppableeee/lan-chat-software-based-on-qt5.git
[email protected]:UnStoppableeee/lan-chat-software-based-on-qt5.git
UnStoppableeee
lan-chat-software-based-on-qt5
毕设大作业基于qt5开发的局域网聊天软件
master

搜索帮助