#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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 1、该资源内项目代码都是经过测试运行成功,功能正常的情况下才上传的,请放心下载使用。 2、适用人群:主要针对计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、数学、电子信息等)的同学或企业员工下载使用,具有较高的学习借鉴价值。 3、不仅适合小白学习实战练习,也可作为大作业、课程设计、毕设项目、初期项目立项演示等,欢迎下载,互相学习,共同进步!
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 37 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
龙年行大运
- 粉丝: 1011
- 资源: 3847
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)