C API 连接MySql

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

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

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"
};
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;
}
}
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;
}
}

运行结果:

posted on 2012-03-22 18:33  imAkaka  阅读(1372)  评论(0编辑  收藏  举报

导航