posts - 18,  comments - 2,  trackbacks - 0
  1 #include <mysql.h>/*注意要包含这个头文件*/
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <stdio.h>
  5 
  6 /*定义了一些数据库连接需要的宏*/
  7 #define HOST "localhost"
  8 #define USERNAME "ABitNo"
  9 #define PASSWORD "ABitNo"
 10 #define DATABASE "abitno"
 11 
 12 /*这个函数用来执行传入的sql語句*/
 13 void exe_sql(char* sql) {
 14 
 15     MYSQL my_connection; /*这是一个数据库连接*/
 16     int res; /*执行sql語句后的返回标志*/
 17 
 18     /*初始化mysql连接my_connection*/
 19     mysql_init(&my_connection);
 20 
 21     /*这里就是用了mysql.h里的一个函数,用我们之前定义的那些宏建立mysql连接,并
 22     返回一个值,返回不为空证明连接是成功的*/
 23     if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE,
 24             0, NULL, CLIENT_FOUND_ROWS)) {/*连接成功*/
 25 
 26         printf("数据库执行exe_sql连接成功!\n");
 27 
 28         /*这句话是设置查询编码为utf8,这样支持中文*/
 29         mysql_query(&my_connection, "set names utf8");
 30 
 31         /*下面这句话就是用mysql_query函数来执行我们刚刚传入的sql語句,
 32         这会返回一个int值,如果为0,证明語句执行成功*/
 33         res = mysql_query(&my_connection, sql);
 34 
 35         if (res) {/*现在就代表执行失败了*/
 36             printf("Error: mysql_query !\n");
 37             /*不要忘了关闭连接*/
 38             mysql_close(&my_connection);
 39         } else {/*现在就代表执行成功了*/
 40             /*mysql_affected_rows会返回执行sql后影响的行数*/
 41             printf("%d 行受到影响!\n\n", mysql_affected_rows(&my_connection));
 42             /*不要忘了关闭连接*/
 43             mysql_close(&my_connection);
 44         }
 45 
 46     } else {
 47         /*数据库连接失败*/
 48         printf("数据库执行exe_sql连接失败!\n");
 49     }
 50 }
 51 
 52 /*这个函数用来执行传入的sql語句,并打印出查询結果*/
 53 void query_sql(char* sql) {
 54     MYSQL my_connection; /*这是一个数据库连接*/
 55     int res; /*执行sql語句后的返回标志*/
 56     MYSQL_RES *res_ptr; /*指向查询结果的指针*/
 57     MYSQL_FIELD *field; /*字段结构指针*/
 58     MYSQL_ROW result_row; /*按行返回的查询信息*/
 59 
 60     int row, column; /*查询返回的行数和列数*/
 61     int i, j; /*只是控制循环的两个变量*/
 62 
 63     /*初始化mysql连接my_connection*/
 64     mysql_init(&my_connection);
 65 
 66     /*这里就是用了mysql.h里的一个函数,用我们之前定义的那些宏建立mysql连接,并
 67     返回一个值,返回不为空证明连接是成功的*/
 68     if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE,
 69             0, NULL, CLIENT_FOUND_ROWS)) {/*Connection success*/
 70 
 71         printf("数据库查询query_sql连接成功!\n");
 72 
 73         /*这句话是设置查询编码为utf8,这样支持中文*/
 74         mysql_query(&my_connection, "set names utf8");
 75 
 76         /*下面这句话就是用mysql_query函数来执行我们刚刚传入的sql語句,
 77         这会返回一个int值,如果为0,证明語句执行成功*/
 78         res = mysql_query(&my_connection, sql);
 79 
 80         if (res) { /*现在就代表执行失败了*/
 81             printf("Error: mysql_query !\n");
 82             /*不要忘了关闭连接*/
 83             mysql_close(&my_connection);
 84         } else { /*现在就代表执行成功了*/
 85             /*将查询的結果给res_ptr*/
 86             res_ptr = mysql_store_result(&my_connection);
 87 
 88             /*如果结果不为空,就把结果print*/
 89             if (res_ptr) {
 90                 /*取得結果的行数和*/
 91                 column = mysql_num_fields(res_ptr);
 92                 row = mysql_num_rows(res_ptr) + 1;
 93                 printf("查询到 %lu 行 \n", row);
 94 
 95                 /*输出結果的字段名*/
 96                 for (i = 0; field = mysql_fetch_field(res_ptr); i++)
 97                     printf("%s\t", field->name);
 98                 printf("\n");
 99 
100                 /*按行输出結果*/
101                 for (i = 1; i < row; i++) {
102                     result_row = mysql_fetch_row(res_ptr);
103                     for (j = 0; j < column; j++)
104                         printf("%s\t", result_row[j]);
105                     printf("\n");
106                 }
107 
108             }
109 
110             /*不要忘了关闭连接*/
111             mysql_close(&my_connection);
112         }
113     }
114 }
115 
116 int main(int argc, char *argv[]) {
117     /*测试下向里面插入数据*/
118     char *exe = "insert into abitno values('ABitNo','http://ABitNo.LinPie.com');";
119     exe_sql(exe);
120 
121     /*测试下查询*/
122     char *query = "select * from abitno;";
123     query_sql(query);
124 
125     return 0;
126 }

转载自:
http://abitno.linpie.com/linux-c-connect-mysql.html

posted on 2009-05-23 21:05 xuejzt 阅读(4591) 评论(1)  编辑 收藏 引用 所属分类: C++

FeedBack:
# re: C语言连接Mysql数据库
2009-06-06 11:43 | ABitNo

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2009年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜