mysql

本文提供了一个使用C语言连接并查询MySQL数据库数据的示例程序,包括连接、查询操作和结果处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何用C语言去访问和查询mysql数据库数据? 第二节 (2012-03-23 21:23:57)转载▼标签: mysql linux c语言连接mysql 分类: MYSQL下面的代码是用C语言连接数据的程序:其中包括连接、查询#include #include #include #include int main(int argc,char *argv[]){MYSQL my_connection;MYSQL_RES *res_ptr;MYSQL_FIELD *fd;MYSQL_ROW sqlrow;int i,j,k;int res;char array[25][25];mysql_init(&my_connection);if(mysql_real_connect(&my_connection,"localhost","root","123456","sg",0,NULL,CLIENT_FOUND_ROWS)){printf("connection success\n");res=mysql_query(&my_connection,"select * from electricity_record");if(res){printf("select error:%S\n",mysql_error(&my_connection));}else{res_ptr=mysql_store_result(&my_connection);if(res_ptr){printf("Retrieved %lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));for(i=0;fd=mysql_fetch_field(res_ptr);i++)strcpy(array[i],fd->name);printf("the information\n");j=mysql_num_fields(res_ptr);for(i=0;i<j;i++)printf("%s\t",array[i]);printf("\n");while(sqlrow=mysql_fetch_row(res_ptr)){for(i=0;i<j;i++){printf("%s\t",sqlrow[i]);}printf("\n");}if(mysql_errno(&my_connection)){fprintf(stderr,"Retrive error:%s\n",mysql_error(&my_connection));}}mysql_free_result(res_ptr);}mysql_close(&my_connection);}else{fprintf(stderr,"Connection failed\n");if(mysql_errno(&my_connection)){fprintf(stderr,"connection error %d:%s\n",mysql_errno(&my_connection),mysql_error(&my_connection));}}return EXIT_SUCCESS;}


http://blog.sina.com.cn/s/blog_609fa94401010jyn.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值