linux下mysql用c语言,插入,删除,查询,实例

来源:互联网 发布:电脑网络共享怎么取消 编辑:程序博客网 时间:2024/06/06 17:24
#include "stdio.h"#include "mysql/mysql.h"#include "string.h"int main(){MYSQL mysql;MYSQL_RES *res=NULL;MYSQL_ROW row=NULL;MYSQL_FIELD* fie;char *str=NULL;int rc,i,fields,rows;if(NULL==mysql_init(&mysql)){printf("mysql_init():%s\n",mysql_error(&mysql));return 1;}if(NULL==mysql_real_connect(&mysql,"localhost","root","","hes",0,NULL,0)){printf("数据库连接失败:%s\n",mysql_error(&mysql));return 1;}printf("数据库连接成功\n");//执行插入请求str="insert into t4 values(21,'dema',2)";rc=mysql_real_query(&mysql,str,strlen(str));if(rc){printf("插入失败%s\n",mysql_error(&mysql));}else printf("插入成功\n");//执行删除str="delete from t4 where id=21";rc=mysql_real_query(&mysql,str,strlen(str));if(rc){printf("删除失败%s\n",mysql_error(&mysql));}else printf("删除成功\n");//执行查询str="select * from t4";rc=mysql_real_query(&mysql,str,strlen(str));if(rc){printf("查询失败%s\n",mysql_error(&mysql));}else printf("查询结果\n");res=mysql_store_result(&mysql);if(!res){printf("mysql_store_result%s\n",mysql_error(&mysql));}rows=mysql_num_rows(res);printf("rows:%d(行)\n",rows);fields=mysql_num_fields(res);printf("fields:%d(列)\n",fields);//打印列头while((fie=mysql_fetch_field(res)))printf("%s\t",fie->name);printf("\n");while((row=mysql_fetch_row(res))){for(i=0;i<fields;i++)printf("%s\t",row[i]);printf("\n");}mysql_free_result(res);mysql_close(&mysql);return 1;}
具体函数功能,可以去看我上篇mysql API
0 0