基于c++ mysql的登录系统代码
时间: 2023-12-23 08:03:01 浏览: 25
以下是一个简单的基于 C++ 和 MySQL 的登录系统代码,需要使用 MySQL Connector/C++ 库来连接 MySQL 数据库:
```cpp
#include <iostream>
#include <string>
#include <mysqlx/xdevapi.h>
using namespace std;
using namespace mysqlx;
int main() {
// 建立连接
Session session("localhost", 3306, "username", "password");
Schema db = session.getSchema("database_name");
// 获取输入的用户名和密码
string username, password;
cout << "请输入用户名:";
cin >> username;
cout << "请输入密码:";
cin >> password;
// 查询数据库,检查用户名和密码是否匹配
Table user_table = db.getTable("user");
Result result = user_table.select().where("username = :username and password = :password")
.bind("username", username).bind("password", password).execute();
// 输出查询结果
if (result.count() > 0) {
cout << "登录成功!" << endl;
} else {
cout << "用户名或密码错误!" << endl;
}
// 关闭连接
session.close();
return 0;
}
```
在上面的代码中,需要将 `username` 和 `password` 替换为你自己的 MySQL 用户名和密码,将 `database_name` 替换为你要连接的数据库名,将 `user` 替换为你的用户表名,并且需要使用 `mysqlcppconn8.lib` 库文件链接 MySQL Connector/C++ 库。此外,为了简化代码,我们并没有进行输入数据的合法性检查,实际应用中需要进行更多的错误处理。