#include "CMYSQL.h"
CCONMYSQL::CCONMYSQL()
{
//Init lib
if (0 == mysql_library_init(0, NULL, NULL)) {
cout << "mysql_library_init succeed" << endl;
}
else {
cout << "mysql_library_init failed" << endl;
}
//init MYSQLDATA;
if (NULL != mysql_init(&mydata)) {
cout << "mysql_init succeed" << endl;
}
else {
cout << "mysql_init failed" << endl;
}
//在连接数据库之前,设置额外的连接选项
//可以设置的选项很多,这里设置字符集,否则无法处理中文
if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk")) {
cout << "mysql_options succeed" << endl;
}
else {
cout << "mysql_options failed" << endl;
}
}
CCONMYSQL::~CCONMYSQL()
{
}
void CCONMYSQL::InitConnectInfor(char *serverip, char *user, char *passwd, char *db, unsigned int port)
{
//Set User Infor
m_ConInfo.serverip = serverip;
m_ConInfo.user = user;
m_ConInfo.password = passwd;
m_ConInfo.database = db;
m_ConInfo.port = port;
}
bool CCONMYSQL::Open()
{
//conncet db
if (NULL!= mysql_real_connect(&mydata, m_ConInfo.serverip, m_ConInfo.user, m_ConInfo.password, m_ConInfo.database,m_ConInfo.port, NULL, 0))
{
cout << "mysql connect succeed" << endl;
return true ;
}
else {
cout << "mysql connect failed" << endl;
if (mysql_errno(&mydata))
{
fprintf(stderr, "(Connection error %d: %s)\n", mysql_errno(&mydata), mysql_error(&mydata));
return false;
}
}
}
bool CCONMYSQL::QueryDatabase(string sql)
{
//结果集
// MYSQL_RES *m_query;
//记录集
// MYSQL_ROW m_row;
//字段数(列)
int iColumn = 0;
//记录数(行)
int iRow = 0;
if (mysql_query(&mydata, sql.c_str()))
{
cout << "Query failed " << mysql_error(&mydata) << endl;
return false;
}
if (!(m_query = mysql_store_result(&mydata))) //获得sql语句结束后返回的结果集
{
return false;
}
// printf("iRrow = %d \n", mysql_affected_rows(&mydata)); //记录行数
printf("iRrow = %d \n", m_query->row_count); //记录行数
printf("iColumn = %d \n", m_query->field_count); //记录列数
iColumn = m_query->field_count;
iRow = m_query->row_count;
//获取字段的信息
char *str_field[256]; //定义一个字符串数组存储字段信息
for (int i = 0; i<iColumn; i++) //在已知字段数量的情况下获取字段名
{
str_field[i] = mysql_fetch_field(m_query)->name;
}
for (int i = 0; i<iColumn; i++) //打印字段
printf("%s\t\t", str_field[i]);
printf("\n");
//打印获取的数据
while (m_row = mysql_fetch_row(m_query)) //在已知字段数量情况下,获取并打印下一行
{
//printf("%10s\t%10s\t%10s\n", m_row[0], m_row[1], m_row[2]); //column是列数组
for (int i = 0; i < iColumn; i++)
{
printf("%s\t\t", m_row[i]);
}
printf("\n");
}
return true;
}
bool CCONMYSQL::ExecuteSql(string SqlQuery)
{
if (mysql_query(&mydata, SqlQuery.c_str()))
{
printf("execute failed (%s)\n", mysql_error(&mydata));
return false;
}
else
{
printf("execute success\n");
return true;
}
}
void CCONMYSQL::Close()
{
//
mysql_close(&mydata);
mysql_server_end();
}
jy_li_
- 粉丝: 0
- 资源: 1
最新资源
- screenrecorder-20241221-204839.mp4
- Screenshot_20241221-204051.png
- 自考计算机网络原理04741真题及答案2018-2020
- YOLO算法-垃圾箱检测数据集-214张图像带标签-垃圾桶.zip
- Hive存储压缩与Hive3性能优化-必看文档
- YOLO算法-施工管理数据集-7164张图像带标签-安全帽-装载机-挖掘机-平地机-移动式起重机-反光背心-工人-推土机-滚筒-哑巴卡车.zip
- YOLO算法-俯视视角草原绵羊检测数据集-4133张图像带标签-羊.zip
- YOLO算法-挖掘机数据集-2656张图像带标签-自卸卡车-挖掘机-轮式装载机.zip
- YOLO算法-火车-轨道-手推车数据集-3793张图像带标签-火车-轨道-手推车.zip
- YOLO算法-垃圾数据集-6561张图像带标签-纸张-混合的-餐厅快餐.zip
- 技术报告:大型语言模型在压力下战略欺骗用户的行为研究
- YOLO算法-水泥路面裂纹检测数据集-5005张图像带标签-裂纹.zip
- YOLO算法-垃圾数据集-568张图像带标签-纸张-纸箱-瓶子.zip
- YOLO算法-施工设备数据集-2000张图像带标签-装载机-挖掘机-平地机-移动式起重机-推土机-滚筒-哑巴卡车.zip
- 防火墙系统项目源代码全套技术资料.zip
- 西门子V90效率倍增-伺服驱动功能库详解-循环通信库 DRIVELib.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈