#include "DBUtil.h"
DBUtil::DBUtil()
{
szUsername = "root";
szPswd = "HANXUEXUN095827";
szHost = "localhost";
szDatabase = "library";
nPort = 3306;
isOpen = false;
}
DBUtil::~DBUtil()
{
CloseDB();
}
//打开数据库
bool DBUtil::OpenDB()
{
mysql_init(&myCont);
if (mysql_real_connect(&myCont, szHost.c_str(), szUsername.c_str(), szPswd.c_str(), szDatabase.c_str(), nPort, NULL, 0))
{
isOpen = true;
return true;
}
return false;
}
//关闭数据库
bool DBUtil::CloseDB()
{
mysql_close(&myCont);//断开连接
return true;
}
bool DBUtil::SelectByName(string name, vector<User>& users)
{
int res;
string sql;
if (isOpen)
{
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
sql += "select * from user where name like '%" + name + "%'";
res = mysql_query(&myCont, sql.c_str());//查询
if (!res)
{
result = mysql_store_result(&myCont);//保存查询到的数据到result
if (result)
{
while (sql_row = mysql_fetch_row(result))//获取具体的数据
{
User user;
user.m_nID = atoi(sql_row[0]);
user.m_strName = sql_row[1];
user.m_nRole = atoi(sql_row[3]);
users.push_back(user);
}
}
}
else
{
cout << "query sql failed!" << endl;
}
}
else
{
cout << "connect failed!" << endl;
}
if (result != NULL)
{
mysql_free_result(result);//释放结果资源
}
return true;
}
bool DBUtil::DeleteByUserId(int id)
{
int res;
string sql;
char szBookId[5] = { 0 };
sprintf(szBookId, "%d", id);
if (isOpen)
{
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
sql += "delete from user where id=";
sql += szBookId;
res = mysql_query(&myCont, sql.c_str());//查询
if (!res)
{
cout << "删除成功!" << endl;
}
else
{
cout << "query sql failed!" << endl;
}
}
else
{
cout << "connect failed!" << endl;
}
cout << "按任意键继续...." << endl;
getchar();
return true;
}
//根据用户名和密码获得用户信息
User DBUtil::ReadUser(string strUserName, string strUserPWD)
{
User user;
char column[32][32];
int res;
string sql;
if (isOpen)
{
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
sql += "select * from user where name='" + strUserName + "' and password='" + strUserPWD + "'";
res = mysql_query(&myCont, sql.c_str());//查询
if (!res)
{
result = mysql_store_result(&myCont);//保存查询到的数据到result
if (result->row_count > 0)
{
int i, j;
//cout << "number of result: " << (unsigned long)mysql_num_rows(result) << endl;
for (i = 0; fd = mysql_fetch_field(result); i++)//获取列名
{
strcpy_s(column[i], fd->name);
}
j = mysql_num_fields(result);
if (j > 0)
{
sql_row = mysql_fetch_row(result);
user.m_nID = atoi(sql_row[0]);
user.m_strName = sql_row[1];
user.m_nRole = atoi(sql_row[3]);
}
}
else
{
user.m_nID = -1;
return user;
}
}
else
{
cout << "query sql failed!" << endl;
}
}
else
{
cout << "connect failed!" << endl;
}
if (result != NULL)
{
mysql_free_result(result);//释放结果资源
}
return user;
}
//新增图书
bool DBUtil::AddBook(Book book)
{
string sql = "";
char szTotal[16];
char szLeftNum[16];
sprintf(szTotal, "%d", book.GetTotalNum());
sprintf(szLeftNum, "%d", book.GetTotalNum());
if (isOpen)
{
//获得时间
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
sql += "insert into book values(null,'" + book.GetBookName() + "','" + book.GetClassify() + "','" + book.GetAuthor() + "','" + book.GetISBN() + "','" + book.GetPub() + "','" + book.GetInDate() + "', " + szTotal + ", " + szLeftNum + ")";
mysql_query(&myCont, sql.c_str());
}
else
{
cout << "connect failed!" << endl;
}
cin.get();
cin.get();
return false;
}
bool DBUtil::BorrowTop(vector<Book>& books)
{
int res;
string sql;
if (isOpen)
{
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
sql += "call proc_1()";
res = mysql_query(&myCont, sql.c_str());//查询
if (!res)
{
result = mysql_store_result(&myCont);//保存查询到的数据到result
if (result)
{
while (sql_row = mysql_fetch_row(result))//获取具体的数据
{
Book book;;
book.SetBookName(sql_row[0]);
book.SetTotalNum(atoi(sql_row[1]));
books.push_back(book);
}
}
}
else
{
cout << "query sql failed!" << endl;
}
}
else
{
cout << "connect failed!" << endl;
}
if (result != NULL)
{
mysql_free_result(result);//释放结果资源
}
return true;
}
//搜索所有图书
bool DBUtil::SelectAllBook(vector<Book>& books)
{
int res;
string sql;
if (isOpen)
{
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
sql += "select * from book";
res = mysql_query(&myCont, sql.c_str());//查询
if (!res)
{
result = mysql_store_result(&myCont);//保存查询到的数据到result
if (result)
{
while (sql_row = mysql_fetch_row(result))//获取具体的数据
{
Book book;
book.SetBookID(atoi(sql_row[0]));
book.SetBookName(sql_row[1]);
book.SetClassify(sql_row[2]);
book.SetAuthor(sql_row[3]);
book.SetISBN(sql_row[4]);
book.SetPub(sql_row[5]);
book.SetInDate(sql_row[6]);
book.SetTotalNum(atoi(sql_row[7]));
book.SetLeftNum(atoi(sql_row[8]));
books.push_back(book);
}
}
}
else
{
cout << "query sql failed!" << endl;
}
}
else
{
cout << "connect failed!" << endl;
}
if (result != NULL)
{
mysql_free_result(result);//释放结果资源
}
return true;
}
//根据书名查询书籍
bool DBUtil::SelectBookByName(string strBookName, vector<Book>& books)
{
int res;
string sql;
if (isOpen)
{
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
sql += "select * from book where bookname like '%" + strBookName + "%'";
res = mysql_query(&myCont, sql.c_str());//查询
if (!res)
{
result = mysql_store_result(&myCont);//保存查询到的数据到result
if (result)
{
while (sql_row = mysql_fetch_row(result))//获取具体的数据
{
Book book;
book.SetBookID(atoi(sql_row[0]));
book.SetBookName(sql_row[1]);
book.SetClassify(sql_row[2]);
book.SetAuthor(sql_row[3]);
book.SetISBN(sql_row[4]);
book.SetPub(sql_row[5]);
book.SetInDate(sql_row[6]);
book.SetTotalNum(atoi(sql_row[7]));
book.SetLeftNum(atoi(sql_row[8]));
books.push_back(book);
}
}
}
else
{
cout << "query sql failed!" << endl;
}
}
else
{
cout << "connect failed!" << endl;
}
if (result != NULL)
{
mysql_free_result(result);//释放结果资源
}
return true;
}
//根据图书ID查询书籍
bool DBUtil::SelectBookById(int nBookId, Book& book)
{
int res;
string sql;
char szBookId[5] = { 0 };
sprintf(szBookId, "%d", nBookId);
if (isOpen)
{
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
sql += "select * from book where id=";
sql += szBookId;
res = mysql_query(&myCont, sql.c_str());//查询
if (!res)
{
result = mysql_store_result(&myCont);//保存查询到的数据到result
if (result)
{
while (sql_row = mysql_fetch_row(result))//获取具体的数据
{
book.SetBookID(atoi(sql_row[0]));
book.SetBookName(sql_row[1]);
book.SetClassify(sql_row[2]);
book.SetAuthor(sql_row[3]);
book.SetISBN(sql_row[4]);
book.SetPub(sql_row[5]);
book.SetInDate(sql_row[6]);
book.SetTotalNum(atoi(sql_row[7]));
book.SetLeftNum(atoi(sql_row[8]));
}
}
}
else
{
cout << "query sql failed!" << endl;
}
}
else
{
cout << "connect failed!" << endl;
}
if (result != NULL)
{
mysql_free_result(result);//释放结果资源
}
return true;
}
//根据ID删除图书
bool DBUtil::DeleteBookById(int nBookId)
{
int res;
string sql;
char szBookId[5] = { 0 };
sprintf(szBookId, "%d", nBookId);
if (isOpen)
{
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
sql += "delete from book where id=";
sql += szBookId;
res = mysql_query(&myCont, sql.c_str());/
manylinux
- 粉丝: 4658
- 资源: 2492
最新资源
- audittest-javaEE框架项目资源
- kde-l10n-Dutch-4.10.5-2.el7.x64-86.rpm.tar.gz
- kde-l10n-Estonian-4.10.5-2.el7.x64-86.rpm.tar.gz
- 基于多目标黏菌优化算法MOSMA的支持向量机SVM参数优化用于多维数据预测回归与负载核预测,利用多目标黏菌优化算法MOSMA优化支持向量机SVM的参数c和g,实现回归预测,可以实现负载核预测,风电等等
- CC智慧物业小程序-活动资源
- kde-l10n-Farsi-4.10.5-2.el7.x64-86.rpm.tar.gz
- KeyMouseHook-活动资源
- KeyMouseHook-活动资源
- 基于PLC和变频器的水蓄冷中央空调控制系统中组态王设计与实现:梯形图程序、接线图与画面展示,组态王设计基于PLC和变频器的水蓄冷中央空调控制系统 带解释的梯形图程序,接线图原理图图纸,io分配,组态画
- Java毕设项目:基于spring+mybatis+maven+mysql实现的演唱会购票管理系统【含源码+数据库+毕业论文】
- kde-l10n-Finnish-4.10.5-2.el7.x64-86.rpm.tar.gz
- MLcore-Engine-机器学习开发资源
- LabVIEW网口TCP通讯实现欧姆龙OMRON PLC全功能解析:源码开放,多类型数据批量读写,无插件胜OPC,原创视频分享,LabVIEW网口TCP通讯欧姆龙OMRON PLC,FINSTCP N
- 基于S7-200PLC的中央空调变频节能控制详解:梯形图程序、接线图与组态画面全解析,S7-200 MCGS 基于S7-200PLC的小区居民楼,宾馆等中央空调变频节能控制 带解释的梯形图程序,接线图
- 每周精选合集-活动资源
- PlaneWars-cocos资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈