我是靠谱客的博主 故意爆米花,最近开发中收集的这篇文章主要介绍mysql哪个版本和x86兼容_[C++]C++连接MySQL,封装为class(兼容x86和x64),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include "CMYSQL.h"#include"stdafx.h"CCONMYSQL::CCONMYSQL()

{//Init lib

if (0 == mysql_library_init(0, NULL, NULL)) {//cout << "初始化库成功" << endl;

}else{//cout << "初始化库失败" << endl;

throw "初始化库失败";

}//init MYSQLDATA;

if (NULL != mysql_init(&mydata)) {//cout << "MySQL初始化成功" << endl;

}else{//cout << "MySQL初始化失败" << endl;

throw "MySQL初始化失败";

}//下面是 在连接数据库之前,设置额外的连接选项

if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "utf8")) { //可以设置的选项很多,这里设置字符集,否则无法处理中文//cout << "MySQL设置成功" << endl;

}else{//cout << "MySQL设置失败" << endl;

throw "MySQL设置失败";

}

}

CCONMYSQL::~CCONMYSQL()

{

}void CCONMYSQL::InitConnectInfor(const char *serverip, const char *user, const char *passwd, const char *db, unsigned intport)

{//初始化数据库连接信息

m_ConInfo.serverip =serverip;

m_ConInfo.user=user;

m_ConInfo.password=passwd;

m_ConInfo.database=db;

m_ConInfo.port=port;

}boolCCONMYSQL::Open()

{//下面是连接数据库

if (NULL!= mysql_real_connect(&mydata, m_ConInfo.serverip, m_ConInfo.user, m_ConInfo.password, m_ConInfo.database,m_ConInfo.port, NULL, 0))

{//cout << "数据库连接成功" << endl;

return true;

}else{//cout << "数据库连接失败" << endl;

throw "数据库连接失败";return false;/*if (mysql_errno(&mydata))

{

fprintf(stderr, "(错误原因 %d: %s)n", mysql_errno(&mydata), mysql_error(&mydata));

return false;

}*/}

}bool CCONMYSQL::QueryDatabase(stringsql)

{if (mysql_query(&mydata, sql.c_str()))

{//cout << "执行查询失败 长时间未连接 连接已丢失" << mysql_error(&mydata) << endl;

throw mysql_error(&mydata);return false;

}if (!(m_query = mysql_store_result(&mydata))) //获得sql语句结束后返回的结果集

{return false;

}//printf("iRrow = %d n", m_query->row_count);//行数、列数为 m_query->field_count

if (m_query->row_count > 0)

{//cout << "真" << endl;

return true;

}else{//cout << "假" << endl;

return false;

}return true;

}string CCONMYSQL::CreateSQL(string text,string uid,string tid,stringshuxing)

{string sql1("SELECT * FROM * WHERE * = '");string sql2("' AND * ='");string sql3("' AND * = '");string sql4("' AND * LIKE '");string sql5("'");string all = sql1 + tid + sql2 + uid + sql3 + shuxing + sql4 + text +sql5;//cout << all;

returnall;

}bool CCONMYSQL::GetAllShuXing(string &shuxing,string text, string tid, string uid,intnumber)

{

InitConnectInfor("ip", "用户名", "密码", "数据库", 3306);char * result = new char[number];for (int i = 0; i < number; i++)

{

result[i]= '0';

}

Open();string sql1("SELECT * FROM * WHERE * = '");string sql2("' AND * ='");string sql3("' AND * LIKE '");string sql4("'");string all = sql1 + tid + sql2 + uid + sql3 + text +sql4;int iColumn = 0;//记录数(行)

int iRow = 0;if (mysql_query(&mydata, all.c_str()))

{return false;

}if (!(m_query = mysql_store_result(&mydata))) //获得sql语句结束后返回的结果集

{return false;

}

iColumn= m_query->field_count;

iRow= m_query->row_count;while (m_row = mysql_fetch_row(m_query)) //在已知字段数量情况下,获取并打印下一行

{for (int i = 0; i < iColumn; i++)

{//printf("%stt", m_row[i]);

int temp =atoi(m_row[i]);

result[temp]= '1';

}

}

shuxing= string(result);

Close();return true;

}bool CCONMYSQL::Search(string text, string uid, string tid, stringshuxing)

{stringSwitch;//string sql = CreateSQL(m_text, m_uid, string(t_tid), string(t_shuxing));

InitConnectInfor("ip", "用户名", "密码", "数据库", 3306);

Open();

SelectValueandSwitch(Switch,text, tid, uid, shuxing);if (Switch == string("1")){return false;

}

string sql =CreateSQL(text, uid, tid, shuxing);if(QueryDatabase(sql.c_str()))

{

Close();return true;

}else{

Close();return false;

}

}bool CCONMYSQL::SelectValueandSwitch(string &Switch, string text, string tid, string uid, stringshuxing)

{string sql1("SELECT * FROM * WHERE * = '");string sql2("'AND * = '");string sql3("' AND * = '");string sql4("' AND * = '");string sql5("'");string all = sql1 + tid + sql2 + uid + sql3 + shuxing +sql4 + text +sql5;if (mysql_query(&mydata, all.c_str()))

{//cout << "Query failed " << mysql_error(&mydata) << endl;

return false;

}if (!(m_query = mysql_store_result(&mydata)))

{return false;

}int Column = m_query->field_count;int Row = (int)m_query->row_count;if (Row > 0)

{

m_row=mysql_fetch_row(m_query);//Value = string(m_row[0]);

Switch = string(m_row[0]);

}else{return false;

}//cout << Value << " " << Switch << endl;

return true;

}void CCONMYSQL::Close() //关闭数据库连接

{

mysql_close(&mydata);

mysql_server_end();

}

最后

以上就是故意爆米花为你收集整理的mysql哪个版本和x86兼容_[C++]C++连接MySQL,封装为class(兼容x86和x64)的全部内容,希望文章能够帮你解决mysql哪个版本和x86兼容_[C++]C++连接MySQL,封装为class(兼容x86和x64)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部