视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
CAPI连接MySql_MySQL
2020-11-09 18:01:04 责编:小采
文档
bitsCN.com

编译环境:WIN7,VS2010,MYSQL版本5.0
因为VS2010自带的CRecordSet类不能与MYSQL5.0匹配,反正我的电脑上是一创建CRecordSet类VS就崩溃,
所以如果想用ODBC接口的话只能直接调用WINAPI,代码既难看又麻烦
于是根据MYSQL自带手册编写了自己的MYSQL类,调试成功,源代码如下:

pse">mysql.h
/* 
* [3/9/2012]
* Powered by akaka
*
* akaka_mysql_h
*
* C API连接MYSQL数据库
* 一、包含/include 和 /lib
* 二、#include (不添加会导致编译时错误)
* 三、添加libmysql.lib
* 四、设置环境变量:这里直接将/lib 目录下的libmysql.dll复制到c:/windows/system32/ 目录下
*
*/
#pragma once
#include "std_lib_facilities.h"
#include
#include

class CMysql{
public:
CMysql();
~CMysql();
void connect(const char *host, const char *user, const char *passwd,
const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag);
void query(const char *query);
void printinfo ();
void printrows ();

private:
MYSQL* _sql;
MYSQL_RES* _sqlres; // result set
private:
unsigned int _column; // how many columns in a row in "result set"
};
pse">mysql.cpp
/* 
* [3/9/2012]
* Powered by akaka
*
*
* akaka_mysql.cpp
*/

#include "akaka_mysql.h"

//------------------------------------------------------------------------------

CMysql::CMysql()
{
if( (_sql = mysql_init(NULL)) == NULL)
{ throw runtime_error("failed in mysql_init!") ;}
}

CMysql::~CMysql()
{
if(_sql)
mysql_close(_sql);
}

//------------------------------------------------------------------------------

void CMysql::connect(const char *host, const char *user, const char *passwd,
const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)
{
if (mysql_real_connect(_sql, host, user, passwd, db,
port, unix_socket, client_flag) == NULL)
throw runtime_error("failed in mysql_connect!");

}

//------------------------------------------------------------------------------
void CMysql::query(const char *query)
{
// mysql_query Returns:
// Zero if the query was successful. Non-zero if an error occurred.
if(mysql_query(_sql, query))
throw runtime_error("failed in mysql_query!");

_sqlres = mysql_store_result(_sql); // 保存结果集
}

//------------------------------------------------------------------------------
void CMysql::printinfo()
{
// count how many columns in a row in "result set"
_column = 0;

MYSQL_FIELD* sqlfield;
while(sqlfield = mysql_fetch_field(_sqlres)) //遍历字段
{
cout << '[' << sqlfield->name << ']';
_column++;
}
cout << endl;
}

//------------------------------------------------------------------------------
void CMysql::printrows()
{
MYSQL_ROW sqlrow;
while( sqlrow = mysql_fetch_row(_sqlres)) // 遍历结果集
{
for(unsigned int i = 0; i < _column; i++) // 遍历每列
{
cout << '[' << sqlrow[i] << ']';
}
cout << endl;
}
}
pse">main.cpp
//------------------------------------------------------------------------------ 
// main
int main()
{
const char* host = "localhost";
const char* user = "root";
const char* passwd = "1121";
const char* db = "my";
const unsigned int port = 3306;
const char* unix_socket = NULL; // If unix_socket is not NULL, the string specifies the socket
// or named pipe that should be used.
// Note that the host parameter determines the type of the connection.
const unsigned long client_flag = 0; // The value of client_flag is usually 0, but can be set to a combination of the following flags to enable certain features:

try
{
CMysql mysql;
mysql.connect(host,user,passwd,db,port,unix_socket,client_flag);
mysql.query("SELECT * FROM myclass");
mysql.printinfo();
mysql.printrows();
return 0;
}catch(runtime_error& e)
{
cerr << e.what() << endl;
return 1;
}
}

运行结果:

bitsCN.com

下载本文
显示全文
专题