#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、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。
资源推荐
资源详情
资源评论
收起资源包目录
基于QT框架的可视化图书管理系统源码(MySQL数据库).zip (37个子文件)
code_20105
borrowbook.ui 2KB
login.cpp 850B
personalmessage.ui 9KB
bookmanager.ui 3KB
book.h 513B
bookmanager.h 650B
user.cpp 4KB
recordmanager.cpp 6KB
returnbook.cpp 6KB
mainpage.h 1KB
book.cpp 2KB
usermanager.h 621B
user.h 522B
returnbook.ui 3KB
mydatabase.cpp 14KB
login.ui 3KB
recordmanager.ui 3KB
sample.pro.user 19KB
usermanager.cpp 3KB
recordmanager.h 608B
personalmessage.cpp 3KB
main.cpp 302B
borrowbook.h 558B
user.ui 8KB
mainpage.ui 7KB
bookmanager.cpp 6KB
personalmessage.h 473B
mydatabase.h 2KB
sample.pro 1KB
usermanager.ui 3KB
book.ui 8KB
mainpage.cpp 3KB
returnbook.h 581B
AHU.png 128KB
borrowbook.cpp 7KB
src.qrc 89B
login.h 367B
共 37 条
- 1
资源评论
土豆片片
- 粉丝: 1844
- 资源: 5824
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功