这几天需要把原来一个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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*=========================================
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if (mysql_real_connect(mysql,host,user,passwd,db,port,0,0))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 0;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return -1;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,执行增、删、改操作
Last:2007-07-30
=========================================*/
int ModifyMySql(MYSQL *mysql, const char *query)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if(mysql_query(mysql,query))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return -1; //failed
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 0;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,执行查询操作
Last:2007-07-30
=========================================*/
int QueryMysql()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//暂无,有需要的只要参考MySql手册中的说明就能写出来
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*=========================================
Author:
Date:2007-07-30
Note:调用MYSQL的C API,退出MYSQL
Last:2007-07-30
=========================================*/
void ExitMySql(MYSQL *mysql)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
mysql_close(mysql);
mysql_server_end();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
MYSQL mysql;
mysql_init(&mysql);
int rtconn=ConnMySql(&mysql,"192.168.6.213","root","xxxx","test",3306);
if(rtconn==-1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
const char * msg = mysql_error(&mysql);
cout << "connect failed:"<<endl;
cout << msg<<endl;
return -1;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout << "connect completed!"<<endl;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int rtmodify=ModifyMySql(&mysql,"insert into forcpp values('cpp',10,'2007-07-10')");
if(rtmodify==-1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
const char * msg = mysql_error(&mysql);
cout << "insert failed:"<<endl;
cout << msg<<endl;
return -1;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout << "insert completed!"<<endl;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ExitMySql(&mysql);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)