mysql example

main
HwangKC 2024-06-05 21:06:46 +08:00
parent 346e8e0511
commit fc5547af37
1 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,123 @@
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/exception.h>
#include <iostream>
#include <string>
#include <random>
std::string generateRandomString(size_t length)
{
const std::string CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<> dist(0, CHARACTERS.size() - 1);
std::string random_string;
for (size_t i = 0; i < length; ++i) {
random_string += CHARACTERS[dist(rng)];
}
return random_string;
}
int main()
{
try
{
// 数据库连接配置
std::string server = "tcp://127.0.0.1:3306";
std::string username = "root";
std::string password = "Hj57471000";
std::string database = "hjems";
// 创建连接
sql::mysql::MySQL_Driver *driver;
driver = sql::mysql::get_mysql_driver_instance();
std::unique_ptr< sql::Connection > con(driver->connect(server, username, password));
// 设置为使用指定数据库
con->setSchema(database);
// 准备SQL查询语句
//std::string sql = "SELECT uid, uname, upasswd, usalt, email, mobile1, mobile2, memo FROM tbl_user WHERE uid = ?";
std::string sql = "SELECT uid, uname, upasswd, usalt, email, mobile1, mobile2, memo FROM tbl_user WHERE uid LIKE ?";
// 创建预编译的prepared statement
std::unique_ptr< sql::PreparedStatement > pstmt(con->prepareStatement(sql));
// 绑定参数
//pstmt->setString(1, "hkc"); // 替换为你要查询的用户名
// 绑定参数,使用 '%' 作为通配符进行模糊匹配
// 例如,搜索以 'user' 开头的所有用户名
std::string pattern = "user%"; // 替换为你要查询的模式
pstmt->setString(1, pattern);
// 执行查询
std::unique_ptr< sql::ResultSet > res(pstmt->executeQuery());
// 处理结果集
while (res->next()) {
// 假设username和password都是字符串类型
std::string user = res->getString("uname");
std::string pass = res->getString("upasswd");
std::string usalt = res->getString("usalt");
std::string email = res->getString("email");
std::string mobile1 = res->getString("mobile1");
std::string mobile2 = res->getString("mobile2");
std::string memo = res->getString("memo");
std::cout << "Username: " << user << ", Password: " << pass << std::endl;
std::cout << "Salt: " << usalt << ", email: " << email << std::endl;
std::cout << "mobile1: " << mobile1 << ", mobile2: " << mobile2 << std::endl;
std::cout << "Memo: " << memo << std::endl;
}
// 插入新用户记录的 SQL 语句
std::string insertSql = "INSERT INTO tbl_user (uid, uname, upasswd, usalt,memo) VALUES (?, ?, ?,?,?)";
// 创建预编译的 prepared statement 对象
std::unique_ptr<sql::PreparedStatement> insertStmt(con->prepareStatement(insertSql));
// 绑定插入数据的参数
size_t passwordLength = 12; // 密码长度不超过12个字符
std::string newUid = generateRandomString(passwordLength);
//const std::string newUid = "new_uid"; // 替换为新用户的用户名
const std::string newSalt = "1C915F2C045EA7628E3CF170BFA59F51"; // 替换为新用户的密码
const std::string newUser = "new_username"; // 替换为新用户的用户名
const std::string newPassword = "new_password"; // 替换为新用户的密码
const std::string newMemo = "新备注信息";
insertStmt->setString(1, newUid);
insertStmt->setString(2, newUser);
insertStmt->setString(3, newPassword);
insertStmt->setString(4, newSalt);
insertStmt->setString(5, newMemo);
// 执行插入操作
int rowsAffected = insertStmt->executeUpdate();
// 检查插入操作是否成功
if (rowsAffected > 0)
{
std::cout << "New user inserted successfully." << std::endl;
} else {
std::cout << "No rows affected during insert operation." << std::endl;
}
// 关闭连接
con->close();
}
catch (sql::SQLException &e) {
std::cerr << "SQLException: " << e.what();
std::cerr << " (MySQL error code: " << e.getErrorCode();
std::cerr << ", SQLState: " << e.getSQLState() << " )" << std::endl;
}
return 0;
}