MySQL——C语言遇上SQL语句

来源:互联网 发布:最新一手淘宝买家数据 编辑:程序博客网 时间:2024/06/09 14:34

前面有一篇博客写了MySQL数据库的基本操作,这里我们在C语言中调用SQL语句,具体代码如下:

#include <mysql/mysql.h>#include <stdio.h>#include <stdlib.h>int main(){MYSQL *conn;//创建一个MYSQL指针MYSQL_RES *result;//保存查询结果MYSQL_ROW row;//从结果中取出行数据MYSQL_FIELD *field;//从结果中取出表头数据conn=mysql_init(NULL);//给指针赋值if(NULL==mysql_real_connect(conn,"localhost","root","","mydatabase",0,NULL,0)){printf("连接错误%s,%d\n",mysql_error(conn),mysql_error(conn));return EXIT_FAILURE;}char *str="select *from my_student1";mysql_set_character_set(conn,"utf8");mysql_query(conn,str);result=mysql_use_result(conn);//从服务器取回刚才查询的结果while(field=mysql_fetch_field(result))//从结果中查出表头数据{printf("%s\t",field->name);}printf("\n");while(row=mysql_fetch_row(result),row!=NULL){printf("%s\n",row[0]);}mysql_free_result(result);mysql_close(conn);}
执行结果如下:



0 0
原创粉丝点击