这几天需要把原来一个C++小程序作数据库移植,从Oracle移到MySql,为了避免安装的麻烦,不想使用ODBC,在网上及MySql手册中查了一下,打算调用其C Api。由于我跟C++或C基本是属于“互不相识”的关系,搞半天竟不知如何在VS中添加引用等等。幸而找到网友beiouwolf的大作,真是很感激:http://blog.csdn.net/beiouwolf/archive/2006/12/22/1453628.aspx。以下部分内容即来源beiouwolf的文章:
(1) 准备工作:从某台装有MySql的机器上,将其安装目录下的“include”、“lib”目录拷到C++项目的目录下。
(2) 项目属性:“C/C++”、“常规”、“附加包含目录”,填上你的mysql/include 目录路径。
(3) 项目属性:“链接器”、“常规”、“附加库目录”,填上你的mysql/lib/debug目录路径。
(4) 项目属性:“链接器”、“输入”、“附加依赖项”,填上libmysql.lib odbc32.lib odbccp32.lib。
程序示例:
#include<winsock2.h>
#include<iostream>
#include "mysql.h"
#include < stdio.h >
…………
using namespace std;
/*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,连接MYSQL数据库
Last:2007-07-30
=========================================*/
int ConnMySql(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port)
{
if (mysql_real_connect(mysql,host,user,passwd,db,port,0,0))
{
return 0;
}
else
{
return -1;
}
}
/*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,执行增、删、改操作
Last:2007-07-30
=========================================*/
int ModifyMySql(MYSQL *mysql, const char *query)
{
if(mysql_query(mysql,query))
{
return -1; //failed
}
else
{
return 0;
}
}
/*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,执行查询操作
Last:2007-07-30
=========================================*/
int QueryMysql()
{
//暂无,有需要的只要参考MySql手册中的说明就能写出来
return 0;
}
/*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,退出MYSQL
Last:2007-07-30
=========================================*/
void ExitMySql(MYSQL *mysql)
{
mysql_close(mysql);
mysql_server_end();
}
int main()
{
MYSQL mysql;
mysql_init(&mysql);
int rtconn=ConnMySql(&mysql,"192.168.6.213","root","xxxx","test",3306);
if(rtconn==-1)
{
const char * msg = mysql_error(&mysql);
cout << "connect failed:"<<endl;
cout << msg<<endl;
return -1;
}
else
{
cout << "connect completed!"<<endl;
}
int rtmodify=ModifyMySql(&mysql,"insert into forcpp values('cpp',10,'2007-07-10')");
if(rtmodify==-1)
{
const char * msg = mysql_error(&mysql);
cout << "insert failed:"<<endl;
cout << msg<<endl;
return -1;
}
else
{
cout << "insert completed!"<<endl;
}
ExitMySql(&mysql);
}
#include<iostream>
#include "mysql.h"
#include < stdio.h >
…………
using namespace std;
/*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,连接MYSQL数据库
Last:2007-07-30
=========================================*/
int ConnMySql(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port)
{
if (mysql_real_connect(mysql,host,user,passwd,db,port,0,0))
{
return 0;
}
else
{
return -1;
}
}
/*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,执行增、删、改操作
Last:2007-07-30
=========================================*/
int ModifyMySql(MYSQL *mysql, const char *query)
{
if(mysql_query(mysql,query))
{
return -1; //failed
}
else
{
return 0;
}
}
/*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,执行查询操作
Last:2007-07-30
=========================================*/
int QueryMysql()
{
//暂无,有需要的只要参考MySql手册中的说明就能写出来
return 0;
}
/*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,退出MYSQL
Last:2007-07-30
=========================================*/
void ExitMySql(MYSQL *mysql)
{
mysql_close(mysql);
mysql_server_end();
}
int main()
{
MYSQL mysql;
mysql_init(&mysql);
int rtconn=ConnMySql(&mysql,"192.168.6.213","root","xxxx","test",3306);
if(rtconn==-1)
{
const char * msg = mysql_error(&mysql);
cout << "connect failed:"<<endl;
cout << msg<<endl;
return -1;
}
else
{
cout << "connect completed!"<<endl;
}
int rtmodify=ModifyMySql(&mysql,"insert into forcpp values('cpp',10,'2007-07-10')");
if(rtmodify==-1)
{
const char * msg = mysql_error(&mysql);
cout << "insert failed:"<<endl;
cout << msg<<endl;
return -1;
}
else
{
cout << "insert completed!"<<endl;
}
ExitMySql(&mysql);
}