#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include "winsock2.h"
#include "mysql.h"
using namespace std;
MYSQL* myConn = NULL;
MYSQL_RES* myRes = NULL;
std::vector<std::string> m_vecFiled;//字段列表数据
std::map<std::string, std::string> m_mapFiled;//字段数据
void GetMysqlFields()
{
m_vecFiled.clear();
m_mapFiled.clear();
//取得结果集中字段的数目
int totalFields = mysql_num_fields(myRes);
for (int i = 0; i < totalFields; ++i)
{
//返回结果集的列,重复调用以检索结果集所有列的信息,没有剩余字段时,返回NULL
// 默认提取下一个尚未被取得的字段
//每次执行新的select查询时将复位
const char* name = mysql_fetch_field(myRes)->name;
std::string fieldName = name;
m_vecFiled.push_back(fieldName);
}
MYSQL_ROW mysqlRow;
//从结果集取得一行作为枚举数组
while ((mysqlRow = mysql_fetch_row(myRes)) != NULL)
{
for (int i = 0; i < totalFields ; ++i)
{
//cout << m_vecFiled[i] << " = " << mysqlRow[i] << "\n";
m_mapFiled.insert(std::make_pair(m_vecFiled[i], mysqlRow[i]));
}
}
}
int RunMysqlStr(const std::string& str)
{
if (str.size() < 1)
{
return -1;
}
int ret = mysql_real_query(myConn, str.c_str(), str.size());
if (ret != 0)
{
return ret;
}
//非查询语句直接返回
auto result = str.find("select");
if (str.find("select") == -1 && str.find("SELECT") == -1)
{
return ret;
}
if (myRes)
{
mysql_free_result(myRes);
myRes = NULL;
}
myRes = mysql_store_result(myConn);
if (myRes == nullptr)
{
return -3;
}
GetMysqlFields();
return 0;
}
void CreateTable()
{
std::string sql = "";
sql = R"(
CREATE TABLE `player` (
`id` bigint(20) UNSIGNED NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`coin` bigint(20) NOT NULL,
`power` int(11) NOT NULL,
`otherdata` blob NOT NULL,
PRIMARY KEY (`id`)
);
)";
auto err = RunMysqlStr(sql);
if (err != 0)
{
printf("CREATE TABLE SUC !\n");
}
else
{
printf("CREATE TABLE FAIL !\n");
}
}
bool RollbackTransaction()
{
std::string str = "ROLLBACK;";
int err = RunMysqlStr(str);
if (err != 0)
{
printf("ROLLBACK FAIL !\n");
return false;
}
printf("ROLLBACK SUC !\n");
return true;
}
bool CommitTransaction()
{
std::string str = "COMMIT;";
int err = RunMysqlStr(str);
if (err != 0)
{
printf("COMMIT FAIL !\n");
return false;
}
printf("COMMIT SUC !\n");
return true;
}
bool StartTransaction()
{
std::string str = "START TRANSACTION;";
int err = RunMysqlStr(str);
if (err != 0)
{
RollbackTransaction();
printf("TRANSACTION FAIL !\n");
return false;
}
printf("TRANSACTION SUC !\n");
return true;
}
std::string EscapeString(const std::string& strSrc)
{
//规定至少开辟str长度*2 + 1的空间存储转换后的字符串
size_t iDstSize = strSrc.size() * 2 + 1;
char* pcDst = new char[iDstSize];
//转换mysql字符串,防止sql注入
int len = mysql_real_escape_string(myConn, pcDst, strSrc.c_str(), strSrc.size());
if (len <= 0)
{
return "";
}
std::string str(pcDst, len);
delete[] pcDst;
pcDst = nullptr;
return "\"" + str + "\"";
}
int EscapeString(const int& value)
{
return value;
}
int WriteMysql()
{
if (!StartTransaction())
{
return -1;
}
const int id = 8;
const std::string name = "jack";
const int coin = 999;
const int power = 2222;
const std::string otherdata = "other";
std::stringstream str;
str.str("");
str << "INSERT INTO player (";
str << "id, name, coin, power, otherdata";
str << ") VALUES (";
str << EscapeString(id) << ",";
str << EscapeString(name) << ",";
str << EscapeString(coin) << ",";
str << EscapeString(power) << ",";
str << EscapeString(otherdata);
str << ") ON DUPLICATE KEY UPDATE ";
str << "name = " << EscapeString(name) << ",";
str << "coin = " << EscapeString(coin) << ",";
str << "power = " << EscapeString(power) << ",";
str << "otherdata = " << EscapeString(otherdata) << ";";
if (RunMysqlStr(str.str()) != 0)
{
RollbackTransaction();
return -2;
}
// 事务提交
if (!CommitTransaction())
return -3;
return 0;
}
int ReadMysql()
{
std::stringstream str;
const int id = 6;
str << "SELECT * FROM player WHERE id = " << EscapeString(id) << ";";
//str << "SELECT * FROM player " << ";";
string name = "";
if (RunMysqlStr(str.str()) != 0)
{
printf("ReadMysql err! \n");
return -1;
}
printf("ReadMysql suc! \n");
for (auto& it : m_mapFiled)
{
cout << it.first << " = " << it.second.data() << "\n";
}
cout << m_mapFiled.find("name")->second.data() << "\n";
return 0;
}
void ConnectMysql()
{
const char* ip = "127.0.0.1";
const char* username = "root";
const char* password = "123456";
const char* dbname = "mytest";
const int port = 3306;
myConn = mysql_init(NULL);
char value = 1;
int time = 5;
mysql_options(myConn, MYSQL_OPT_RECONNECT, &value);
mysql_options(myConn, MYSQL_OPT_CONNECT_TIMEOUT, &time);
mysql_options(myConn, MYSQL_SET_CHARSET_NAME, "utf8");
if (mysql_real_connect(myConn, ip, username, password, dbname, port, NULL, 0))
{
printf("connect success !\n");
CreateTable();
}
}
int main()
{
mysql_library_init(0, NULL, NULL);
ConnectMysql();
WriteMysql();
ReadMysql();
//释放结果资源
if (myRes)
{
mysql_free_result(myRes);
}
mysql_close(myConn);//关闭MySQL连接
mysql_library_end();//关闭MySQL库
system("pause");
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
mysql 5.7库文件和测试代码
共109个文件
h:101个
pp:6个
lib:1个
需积分: 0 0 下载量 117 浏览量
2023-07-10
15:03:01
上传
评论
收藏 309KB RAR 举报
温馨提示
mysql
资源推荐
资源详情
资源评论
收起资源包目录
mysql 5.7库文件和测试代码 (109个子文件)
TestMysql.cpp 6KB
mysqld_ername.h 123KB
psi.h 85KB
mysqld_error.h 46KB
my_sys.h 39KB
mysql_file.h 39KB
m_ctype.h 34KB
mysql_thread.h 33KB
mysql_socket.h 33KB
mysql.h 29KB
plugin.h 26KB
plugin.h 26KB
mysql_com.h 25KB
my_global.h 23KB
plugin_audit.h 18KB
plugin_audit.h 18KB
sql_state.h 15KB
service_command.h 14KB
my_config.h 13KB
plugin_trace.h 12KB
m_string.h 11KB
my_dbug.h 10KB
service_parser.h 10KB
sql_common.h 8KB
plugin_ftparser.h 8KB
plugin_ftparser.h 8KB
keycache.h 8KB
mysql_transaction.h 7KB
client_plugin.h 7KB
mysql_statement.h 7KB
my_getopt.h 7KB
plugin_keyring.h 7KB
plugin_keyring.h 7KB
thread_pool_priv.h 6KB
thr_rwlock.h 6KB
my_byteorder.h 6KB
plugin_group_replication.h 6KB
plugin_group_replication.h 6KB
thr_mutex.h 6KB
my_compiler.h 6KB
plugin_auth.h 6KB
service_mysql_string.h 6KB
service_srv_session_info.h 6KB
decimal.h 5KB
service_ssl_wrapper.h 5KB
thr_cond.h 5KB
plugin_auth_common.h 5KB
service_rules_table.h 5KB
service_srv_session.h 5KB
my_thread.h 5KB
mysql_stage.h 5KB
group_replication_priv.h 5KB
errmsg.h 5KB
psi_memory.h 5KB
service_locking.h 5KB
service_thd_alloc.h 5KB
service_security_context.h 4KB
mysql_table.h 4KB
service_thd_wait.h 4KB
psi_base.h 4KB
mysql_ps.h 4KB
big_endian.h 4KB
little_endian.h 4KB
service_my_snprintf.h 4KB
mysql_mdl.h 4KB
service_rpl_transaction_write_set.h 4KB
com_data.h 3KB
my_dir.h 3KB
sslopt-vars.h 3KB
mysql_sp.h 3KB
my_xml.h 3KB
sslopt-longopts.h 3KB
service_mysql_alloc.h 3KB
service_thread_scheduler.h 3KB
my_alloc.h 3KB
mysql_idle.h 3KB
my_thread_local.h 3KB
typelib.h 3KB
byte_order_generic.h 3KB
byte_order_generic_x86.h 3KB
service_mysql_keyring.h 3KB
service_mysql_password_policy.h 3KB
mysql_time.h 3KB
service_rpl_transaction_ctx.h 3KB
binary_log_types.h 3KB
innodb_priv.h 3KB
sslopt-case.h 2KB
services.h 2KB
my_command.h 2KB
my_list.h 2KB
service_my_plugin_log.h 2KB
mysql_memory.h 2KB
plugin_validate_password.h 2KB
plugin_validate_password.h 2KB
service_thd_engine_lock.h 2KB
thread_type.h 2KB
mysql_com_server.h 2KB
client_authentication.h 2KB
get_password.h 2KB
mysql_embed.h 2KB
共 109 条
- 1
- 2
资源评论
moon894295412
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功