c语言连接mysql数据库的实现方法

来源:互联网 发布:风力发电现状 知乎 编辑:程序博客网 时间:2024/06/05 00:15

投稿:mdxy-dxy 字体:[增加 减小] 类型:转载 时间:2012-05-05 我要评论

C语言连接mysql数据库,需要相应的头文件和lib文件,如果你安装Mysql数据库,会在安装目录下找到这些库文件,如果没有安装,也可以在网上找到

我这里也有一份网上找到的:/201205/other/C_link_mySql51.rar 
C连接MySql5.1所需文件.rar 
附带一个不错的例子: 

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