#include "mydatabase.h"
//静态变量不能在头文件中进行初始化,哪怕是在头文件的类外中也不行,因为这会造成头文件被多次包含时静态变量被多次定义,所以只能在源文件中初始化
MyDataBase* MyDataBase::myDataBase=nullptr;
MyDataBase *MyDataBase::getDataBase()
{
if(!myDataBase)
{
myDataBase=new MyDataBase();
return myDataBase;
}
else
{
return myDataBase;
}
}
QVector<QVector<QVariant> > MyDataBase::getReader(QString condition)
{
QString str="";
if(power)
{
str="select * from Reader where Id like '%"+condition+"%' or ReaderName like '%"+condition
+"%' or sex = '"+condition+"' or age = '"+condition+"' or contact like '%"+condition+"%'";
}
else
{
str="select * from Reader where Id = '"+Id+"'";
}
// qDebug()<<condition;
// qDebug()<<str;
QSqlQuery sql(db);
sql.exec(str);
QVector<QVector<QVariant>> vector_1;
while(sql.next())
{
QVector<QVariant> vector_2;
for(int i=0;i<8;i++)
{
vector_2.push_back(sql.value(i));
}
vector_1.push_back(vector_2);
}
return vector_1;
}
QVector<QVector<QVariant>> MyDataBase::getBook(QString condition)
{
//这里不能使用QString::asprintf函数来创建QString,因为condition为数字字符串时,%%转义为%后会把填充的数字字符串作为占位符
QString str="select * from Book where BookId like '%"+condition+"%' or BookName like '%"+condition+"%' or Author like '%"+condition+"%' "
"or Press like '%"+condition+"%' or Kind like '%"+condition+"%'";
// qDebug()<<condition;
// qDebug()<<str;
QSqlQuery sql(db);
sql.exec(str);
QVector<QVector<QVariant>> vector_1;
while(sql.next())
{
QVector<QVariant> vector_2;
for(int i=0;i<7;i++)
{
vector_2.push_back(sql.value(i));
}
vector_1.push_back(vector_2);
}
return vector_1;
}
//存在问题,应该区分读者和管理员获取数据
//已解决一部分,还需解决获取管理员记录数据的问题
QVector<QVector<QVariant>> MyDataBase::getRecord(QString condition)
{
QString str="";
if(power)
{
//一次执行多条搜索语句,只会返回第一条的搜索结果,所以要分开搜索
str="SELECT * FROM readerrecord WHERE RecordId LIKE '%"+condition+"%' OR BookId LIKE '%"
+condition+"%' OR BookName LIKE '%"+condition+"%' OR Kind LIKE '%"
+condition+"%' OR ReaderId LIKE '%"+condition+"%' OR ReaderName LIKE '%"
+condition+"%' OR BorrowTime LIKE '%"+condition+"%' OR ReturnTime LIKE '%"+condition+"%';";
}
else
{
str="SELECT * FROM readerrecord WHERE ( RecordId LIKE '%"+condition+"%' OR BookId LIKE '%"
+condition+"%' OR BookName LIKE '%"+condition+"%' OR Kind LIKE '%"
+condition+"%') and ReaderId = '"+Id+"' and ReaderName = '"
+name+"' and ( BorrowTime LIKE '%"+condition+"%' OR ReturnTime LIKE '%"+condition+"%' )";
}
// qDebug()<<condition;
// qDebug()<<str;
QSqlQuery sql(db);
sql.exec(str);
QVector<QVector<QVariant>> vector_1;
while(sql.next())
{
QVector<QVariant> vector_2;
for(int i=0;i<10;i++)
{
vector_2.push_back(sql.value(i));
}
vector_1.push_back(vector_2);
}
//如果是管理员,还得获取管理员记录表数据
if(power)
{
str="SELECT * FROM managerrecord WHERE RecordId LIKE '%"+condition+"%' OR BookId LIKE '%"
+condition+"%' OR BookName LIKE '%"+condition+"%' OR Kind LIKE '%"
+condition+"%' OR ManagerId LIKE '%"+condition+"%' OR ManagerName LIKE '%"
+condition+"%' OR BorrowTime LIKE '%"+condition+"%' OR ReturnTime LIKE '%"+condition+"%';";
//str="select * from managerrecord;";
sql.exec(str);
qDebug()<<"AAA";
while(sql.next())
{
QVector<QVariant> vector_2;
for(int i=0;i<10;i++)
{
vector_2.push_back(sql.value(i));
}
vector_1.push_back(vector_2);
}
}
return vector_1;
}
//后期完善登录后可以换不同用户来检测
QVector<QVector<QVariant> > MyDataBase::getReturnRecord(QString condition)
{
QString str="select RecordId,BookId,BookName,Kind,ReaderId,ReaderName,BorrowTime,Deadline from ReaderRecord ";
//sql语句中and的优先级高于or
if(!power)
{
str+="where ReaderId = '"+Id+"' and ReturnTime = '' and (RecordId like '%"+condition+"%' or BookId like '%"
+condition+"%' or BookName like '%"+condition+"%' or kind like '%"+condition+"%' or BorrowTime like '%"+condition+"%')";
}
else
{
str+="where ReturnTime = '' and (ReaderId like '%"+condition+"%' or RecordId like '%"+condition+"%' or BookId like '%"+condition+"%' or ReaderName like '%"
+condition+"%' or BookName like '%"+condition+"%' or kind like '%"+condition+"%' or BorrowTime like '%"+condition+"%')";
}
//qDebug()<<str;
QSqlQuery sql(db);
sql.exec(str);
QVector<QVector<QVariant>> vector_1;
while(sql.next())
{
QVector<QVariant> vector_2;
for(int i=0;i<8;i++)
{
vector_2.push_back(sql.value(i));
}
vector_1.push_back(vector_2);
}
//获取管理员的还书信息
if(power)
{
str="select RecordId,BookId,BookName,Kind,ManagerId,ManagerName,BorrowTime,Deadline from ManagerRecord "
"where ReturnTime = '' and (ManagerId like '%"+condition+"%' or RecordId like '%"+condition+"%' or BookId like '%"+condition+"%' or ManagerName like '%"
+condition+"%' or BookName like '%"+condition+"%' or kind like '%"+condition+"%' or BorrowTime like '%"+condition+"%')";
sql.exec(str);
while(sql.next())
{
QVector<QVariant> vector_2;
for(int i=0;i<8;i++)
{
vector_2.push_back(sql.value(i));
}
vector_1.push_back(vector_2);
}
}
return vector_1;
}
void MyDataBase::borrowBook(QString str1,QString str2,QString str3,QString str4)
{
QString str5=Id;
QString str6=name;
QString str7=QDate::currentDate().toString("yyyy.M.d");
QString str8="";
QString str9="0";
QString str10="30";
QString str="";
if(power)
{
str=QString::asprintf("insert into ManagerRecord values('%1','%2','%3','%4','%5','%6','%7','%8',%9,%10);"
"update Book set quantity=quantity-1 where BookId='%11';")
.arg(str1).arg(str2).arg(str3).arg(str4).arg(str5).arg(str6).arg(str7)
.arg(str8).arg(str9).arg(str10).arg(str2);
}
else
{
str=QString::asprintf("insert into ReaderRecord values('%1','%2','%3','%4','%5','%6','%7','%8',%9,%10);"
"update Book set quantity=quantity-1 where BookId='%11';")
.arg(str1).arg(str2).arg(str3).arg(str4).arg(str5).arg(str6).arg(str7)
.arg(str8).arg(str9).arg(str10).arg(str2);
}
QSqlQuery sql(db);
sql.exec(str);
// if(sql.exec(str))
// qDebug()<<"借阅成功";
// else
// {
// qDebug()<<"借阅失败";
// qDebug()<<db.lastError().text();
// qDebug()<<str;
// }
}
void MyDataBase::returnBook(QString str1,QString str2,QString str3)
{
QString str="update ";
QString returnTime=QDate::currentDate().toString("yyyy.MM.dd");
QSqlQuery sql(db);
if(str3[0]=='U')
{
str+="ReaderRecord set ReturnTime = '"+returnTime+"' where RecordId = '"+str1+"'; update Book set quantity=quantity+1 where BookId='"+str2+"';";
}
else
{
str+="ManagerRecord set ReturnTime = '"+returnTime+"' where RecordId = '"+str1+"'; update Book
土豆片片
- 粉丝: 1854
- 资源: 5869
最新资源
- Call The Roll.zip
- java课程设计-基于SSM框架的酒店客房管理系统源码+数据库脚本
- 9013,8550等SPICE模型
- 基于大数据、人工智能的招聘大数据分析展示系统-前端可视化.zip
- 具有执行器饱和及故障的航天器姿态主动容错控制;容错控制;航天器姿态;执行器故障及饱和
- (2025)Microsoft.WorldLockingTools插件Unity版本v2.7.3完整版(包括四个包)
- comsol复合材料各向异性相场模型,采用固体力学、亥姆霍兹pde接口,各向异性断裂能,提供mph文件
- matlab仿真,级联h桥储能变流器,下垂控制离网运行仿真,vsg控制,同步发电机控制,离网模式并网模式下均可以运行,可以实现预同步,实现平滑并网
- Flink流批一体数据处理快速集成开发框架 不仅能够快速构建基于Java的Flink流批一体应用程序,实现异构数据库实时同步和ETL,还可以让Flink SQL变得极其简单,玩转Flink
- 复旦大数据学院课程作业人工智能,分布式系统,自然语言处理,高级大数据解析,计算机网络,数据可视化.zip
- 单极倍频载波移相调制matlab仿真,9级联,19电平
- (2025)Unity图像识别包 OpenCV for Unity 2.6.0
- 大数据分析屏01234.zip
- Openmetadata之1.6.1最新版本安装-yellowcong
- 基于非奇异终端滑模控制的二自由度机械臂系统 1理论说明文档:进行机械臂轨迹跟踪控制器理论设计说明 2NTSMC控制:是通过引入非线性辅助项,将滑模控制器的终端项从奇异点(可能导致系统决策和切问
- 大数据存储与管理 (华中科技大学计算机科学与技术学院).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈