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 name _column++;
}
cout }
//------------------------------------------------------------------------------
void cmysql::printrows()
{
mysql_row sqlrow;
while( sqlrow = mysql_fetch_row(_sqlres)) // 遍历结果集
{
for(unsigned int i = 0; i {
cout }
cout }
}
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 return 1;
}
}
运行结果:
bitscn.com