转载自:http://blog.csdn.net/pdw2009/article/details/22224825
二、安装mysql开发包
sudo apt-get install libmysqlclient-dev
Ubuntu Package查询地址(输入关键词查询到最新的package名字,比如libmysqlclient,然后装最新的版本):
三、如何调用MYSQL库文件
加入头文件: #include <mysql/mysql.h>
#include <mysql/mysql.h>
#include "stdio.h"
int main()
{
printf("i love c\n");
return 0;
}
保存为test.c
四、编译方法
gcc -o test test.c -lmysqlclient
或(c++程序)
g++ -o test test.cpp -lmysqlclient
还是找不到mysql.h?添加参数-I/usr/include/mysql/看看(示例如下),假设mysql.h在该目录下。
(另外参考http://bbs.csdn.net/topics/390406501解决方案,我是这样解决的)
gcc -o test test.c -lmysqlclient -I/usr/include/mysql/
执行:
./test
例子
自我感觉:例子封装不好,做成数据库封装:例如传递 MYSQL ROW等 做为函数参数,并且传递指针
具体封装内容待定!!
MYSQL DbObj; //handle
MYSQL_RES *pRes; //result
MYSQL_ROW sqlrow; //row
int main()
{
char strsql[200];
char username[20];
char password[20];
sprintf(username,"root");
sprintf(password,"123456");
mysql_init(&DbObj);
if(!mysql_real_connect(&DbObj,"192.168.75.131",username,password,"test",0,NULL,0) )
{
return 0;
}
sprintf(strsql,"select * from person");
int res=mysql_query(&DbObj,strsql);
pRes=mysql_use_result(&DbObj);
if( (sqlrow=mysql_fetch_row(pRes))!=NULL )
{
mysql_free_result(pRes);//释放记录集
printf("Ok.\n");
}
mysql_close(&DbObj);
}
再次改进,读字段名和记录
#include <mysql/mysql.h>
#include "stdio.h"
#include <string.h>
MYSQL DbObj; //handle
MYSQL_RES *pRes; //result
MYSQL_ROW sqlrow; //row
void display_row();
void display_head();
int main()
{
char strsql[200];
char username[20];
char password[20];
int first_row=1;
sprintf(username,"root");
sprintf(password,"123456");
mysql_init(&DbObj);
if(!mysql_real_connect(&DbObj,"192.168.75.131",username,password,"test",0,NULL,0) )
{
return 0;
}
sprintf(strsql,"select * from person");
int res=mysql_query(&DbObj,strsql);
if(res)
{
fprintf(stderr,"select error: %s\n",mysql_error(&DbObj) );
}
else
{
pRes=mysql_use_result(&DbObj);
if(pRes)
{
while( (sqlrow=mysql_fetch_row(pRes))!=NULL )
{
if(first_row)
{
display_head();
first_row=0;
}
display_row();
}
}
}
mysql_close(&DbObj);
}
/*
读字段名
*/
void display_head()
{
MYSQL_FIELD *field_ptr;
printf("column details:\n");
while( (field_ptr=mysql_fetch_field(pRes))!=NULL )
{
printf("Name:%s\t\n",field_ptr->name);
printf("Type:\t" );
if(IS_NUM(field_ptr->type))
{
printf("Numeric filed\n");
}
else
{
switch(field_ptr->type)
{
case FIELD_TYPE_VAR_STRING:
printf("varchar \n");
break;
case FIELD_TYPE_LONG:
printf("Long \n");
break;
default:
printf("Type is %d,check in mysql_com.h\n",field_ptr->type);
}
}
}
}
/*
读行记录
*/
void display_row()
{
unsigned int field_count;
field_count = 0;
while(field_count<mysql_field_count(&DbObj))
{
if(sqlrow[field_count]) printf("%s ",sqlrow[field_count]);
else printf("NULL");
field_count++;
}
printf("\n");
}
继续改进,加上事务,加上执行insert ,update
使用的前提是创建表的时候TYPE=INNODB
#include <mysql/mysql.h>
#include "stdio.h"
#include <string.h>
MYSQL DbObj; //handle
MYSQL_RES *pRes; //result
MYSQL_ROW sqlrow; //row
void display_row();
void display_head();
int main()
{
char strsql[200];
char insrtsql[200];
char username[20];
char password[20];
int first_row=1;
int t=-1;
sprintf(username,"root");
sprintf(password,"lovepxm");
mysql_init(&DbObj);
if(!mysql_real_connect(&DbObj,"192.168.75.131",username,password,"test",0,NULL,0) )
{
fprintf(stderr,"无法连接到数据库,错误原因是:%s/n",mysql_error(&DbObj));
return 0;
}
sprintf(insrtsql,"%s","insert into person (person_name) Values ('xx')");
mysql_query(&DbObj,insrtsql);
t=mysql_real_query(&DbObj,"SET AUTOCOMMIT =0",(unsigned int)strlen("SET AUTOCOMMIT =0"));
if(t){
printf("启用手工事务失败\n");
}else{
printf("启用手工事务成功\n");
}
t=mysql_real_query(&DbObj,"Begin ;",(unsigned int)strlen("Begin ;")); //开启事务
sprintf(insrtsql,"%s","insert into person (person_name) Values ('yyyy')");
int inset_1=mysql_query(&DbObj,insrtsql);
sprintf(insrtsql,"%s","insert into person (person_name) Values ('ok')");
int inset_2=mysql_query(&DbObj,insrtsql);
if(inset_1==0 && inset_2==0 ){ //结束事务
printf("事务提交\n");
t=mysql_real_query(&DbObj,"COMMIT;",(unsigned int)strlen("COMMIT;"));
}else{
printf("事务回滚\n");
t=mysql_real_query(&DbObj,"ROLLBACK;",(unsigned int)strlen("ROLLBACK;"));
}
sprintf(strsql,"select * from person");
int res=mysql_query(&DbObj,strsql);
if(res)
{
fprintf(stderr,"select error: %s\n",mysql_error(&DbObj) );
}
else
{
pRes=mysql_use_result(&DbObj);
if(pRes)
{
while( (sqlrow=mysql_fetch_row(pRes))!=NULL )
{
if(first_row)
{
display_head();
first_row=0;
}
display_row();
}
}
}
mysql_close(&DbObj);
}
/*
读字段名
*/
void display_head()
{
MYSQL_FIELD *field_ptr;
printf("column details:\n");
while( (field_ptr=mysql_fetch_field(pRes))!=NULL )
{
printf("Name:%s\t\n",field_ptr->name);
printf("Type:\t" );
if(IS_NUM(field_ptr->type))
{
printf("Numeric filed\n");
}
else
{
switch(field_ptr->type)
{
case FIELD_TYPE_VAR_STRING:
printf("varchar \n");
break;
case FIELD_TYPE_LONG:
printf("Long \n");
break;
default:
printf("Type is %d,check in mysql_com.h\n",field_ptr->type);
}
}
}
}
/*
读行记录
*/
void display_row()
{
unsigned int field_count;
field_count = 0;
while(field_count<mysql_field_count(&DbObj))
{
if(sqlrow[field_count]) printf("%s ",sqlrow[field_count]);
else printf("NULL");
field_count++;
}
printf("\n");
}
http://www.metsky.com/archives/554.html
http://www.cnblogs.com/co1d7urt/archive/2012/10/23/2735320.html