一、sql.h
#ifndef SQL_H
#define SQL_H
#include <mysql.h>
#include <iostream>
typedef struct Data {
std::string username;
std::string password;
}Data;
class Sql {
public:
Sql(); //初始化
~Sql(); //释放空间
bool connect(); //连接数据库
bool login(); //登录
bool re(); //注册
bool correct(); //修改密码
bool logout(); //注销账户
private:
MYSQL mysql;
MYSQL_RES* result;
MYSQL_ROW sql_row;
Data data;
};
#endif //sql.h
二、sql.cpp
#include "sql.h"
#include <string>
#include <sstream>
using std::cout;
using std::endl;
using std::string;
using std::cin;
Sql::Sql() {
/*初始化*/
mysql_init(&mysql);
result = nullptr;
sql_row = nullptr;
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "GBK");
}
Sql::~Sql() {
/*释放空间、断开连接*/
mysql_free_result(result);
mysql_close(&mysql);
cout << "数据库已断开!" << endl;
}
bool Sql::connect() {
/*连接数据库*/
if (mysql_real_connect(&mysql, "localhost", "root", "root", "db_study", 3306, NULL, 0)) {
cout << "数据库连接成功!" << endl;
return true;
}
else {
cout << "数据库连接失败!" << endl;
cout << "error: " << mysql_error(&mysql) << endl;
return false;
}
}
bool Sql::login() {
if (!mysql_query(&mysql, "select * from name_password")) {
result = mysql_store_result(&mysql); //得到结果集
cout << "结果集数量为:" << mysql_num_rows(result) << endl;
if (result) {
/*检查用户名*/
cout << "请输入您的用户名:";
string name;
cin >> name;
cin.ignore();
while (sql_row = mysql_fetch_row(result)) {
if (sql_row[0] == name) {
/*用户名存在检查密码*/
cout << "请输入您的密码:";
string password;
getline(cin, password);
if (sql_row[1] == password) return true;
else { cout << "密码错误!" << endl; return false; }
}
}
}
}
/*用户名不存在*/
cout << "用户名不存在!" << endl;
return false;
}
bool Sql::re() {
string name;
if (!mysql_query(&mysql, "select * from name_password")) {
result = mysql_store_result(&mysql);
cout << "结果集数量为:" << mysql_num_rows(result) << endl;
if (result) {
/*检查用户名*/
cout << "请设置您的用户名:";
cin >> name;
cin.ignore();
while (sql_row = mysql_fetch_row(result)) {
if (sql_row[0] == name) { cout << "用户名重复!" << endl; return false; }
}
}
}
/*检验密码*/
while (1) {
cout << "请设置密码:";
string password;
getline(cin, password);
cout << "请再次输入密码:";
string check_password;
getline(cin, check_password);
if (password == check_password) {
/*存入用户名和密码*/
std::stringstream oss;
oss << "insert into name_password(name,password) values('" << name << "','" << password << "');";
string temp = oss.str();
const char* query = temp.data();
if (!mysql_query(&mysql, query)) return true;
else { cout << "error reason: " << mysql_error(&mysql) << endl; return false; }
break;
}
else cout << "密码不一致!" << endl;
}
return true;
}
bool Sql::correct() {
string name;
if (!mysql_query(&mysql, "select * from name_password")) {
result = mysql_store_result(&mysql);
cout << "结果集数量为:" << mysql_num_rows(result) << endl;
if (result) {
/*检查用户名*/
cout << "请输入您的用户名:";
cin >> name;
cin.ignore();
while (sql_row = mysql_fetch_row(result)) {
if (sql_row[0] == name) {
/*用户名存在*/
string password;
string check_password;
while (1) {
cout << "请设置新密码:";
getline(cin, password);
cout << "请重新输入密码:";
getline(cin, check_password);
if (password == check_password) {
/*密码检查一致*/
std::stringstream oss;
oss << "update name_password set password = '" << password << "' where name = '" << name << "';";
string temp = oss.str();
const char* query = temp.data();
if (!mysql_query(&mysql, query)) return true;
else { cout << "error reason: " << mysql_error(&mysql) << endl; return false; }
}
else
cout << "密码不一致!" << endl;
}
}
}
}
}
/*用户名不存在*/
cout << "用户名不存在!" << endl;
return false;
}
bool Sql::logout() {
string name;
if (!mysql_query(&mysql, "select * from name_password")) {
result = mysql_store_result(&mysql);
cout << "结果集数量为:" << mysql_num_rows(result) << endl;
if (result) {
cout << "请输入您要注销的用户名:";
cin >> name;
while (sql_row = mysql_fetch_row(result)) {
if (sql_row[0] == name) {
/*用户名存在*/
std::stringstream oss;
oss << "delete from name_password where name = '" << name << "';";
string temp = oss.str();
const char* query = temp.data();
if (!mysql_query(&mysql, query)) {
/*删除成功,重新排序*/
if (!mysql_query(&mysql, "alter table name_password auto_increment = 1;")) {
cout << "重新排序成功!" << endl;
}
else { cout << "重新排序失败!" << endl; cout << "error reason: " << mysql_error(&mysql) << endl; }
return true;
}
else { cout << "error reason: " << mysql_error(&mysql) << endl; return false; }
}
}
}
}
/*用户名不存在*/
cout << "用户名不存在!" << endl;
return false;
}
三、main.cpp
#include "sql.h"
using std::cout;
using std::endl;
using std::cin;
int main() {
Sql sql; //对象
if (sql.connect()) {
while (1) {
cout << "****************" << endl;
cout << "* 1.登录 *" << endl;
cout << "* 2.注册 *" << endl;
cout << "* 3.修改密码 *" << endl;
cout << "* 4.注销账户 *" << endl;
cout << "****************" << endl;
int order;
cout << "请输入您的指令:";
cin >> order;
switch (order) {
case 1:
if (sql.login()) {
cout << "登录成功!" << endl;
/*登录成功后的界面*/
}
else cout << "登录失败!" << endl;
cout << endl;
break;
case 2:
if (sql.re()) cout << "注册成功!" << endl;
else cout << "注册失败!" << endl;
cout << endl;
break;
case 3:
if (sql.correct()) cout << "修改成功!" << endl;
else cout << "修改失败!" << endl;
cout << endl;
break;
case 4:
if (sql.logout()) cout << "账户注销成功!" << endl;
else cout << "账户注销失败!" << endl;
cout << endl;
break;
default: cout << "输入的指令不存在!" << endl << endl; break;
}
}
}
else return 1;
return 0;
}
四、配置:
包含目录:MYSQL\include
库目录:MYSQL\lib
调试环境:PATH=...\MYSQL\bin
链接器->输入->附加依赖项:libmysql.lib
将libmysql.dll添加到项目文件夹中