mysql

来源:互联网 发布:淘宝客提现规则 编辑:程序博客网 时间:2024/06/03 23:46

如何用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

0 0