-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectdata.c
More file actions
40 lines (38 loc) · 1020 Bytes
/
selectdata.c
File metadata and controls
40 lines (38 loc) · 1020 Bytes
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
#include<stdio.h>
#include<mysql.h>
int main(){
//创建mysql描述符
MYSQL* mysql_fd = mysql_init(NULL);
//连接数据库
if(mysql_real_connect(mysql_fd,"127.0.0.1","root","","bit",3306,NULL,0) ==NULL){
printf("connect failed\n");
return 1;
}
//连接成功
char buf[1024]= {};
sprintf(buf,"select * from bit2");
mysql_query(mysql_fd,buf);
MYSQL_RES *res = mysql_store_result(mysql_fd);
int row = mysql_num_rows(res);
int col = mysql_num_fields(res);
MYSQL_FIELD * field = mysql_fetch_field(res);
int i = 0;
for(;i<col;i++){
printf("%s\t",field[i].name);
}
printf("\n");
printf("<table border=\"1\">");
for(i = 0;i<row;++i){
int j = 0;
MYSQL_ROW rowdata = mysql_fetch_row(res);
printf("<tr>");
for(;j<col;++j){
printf("<td>%s<td>",rowdata[j]);
}
printf("</tr>");
}
printf("</table>");
mysql_close(mysql_fd);
printf("OK!");
return 0;
}