-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathselect4.c
executable file
·96 lines (89 loc) · 2.7 KB
/
select4.c
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
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
void display_row();
void display_header();
int main(int argc, char *argv[])
{
int res;
int first_row = 1;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "localhost", "root",
"root", "test", 0, NULL, 0)) {
printf("Connection success\n");
res = mysql_query(&my_connection,
"select childno, fname, age from children where age > 5");
if (res) {
printf("select error: %s\n", mysql_error(&my_connection));
} else {
res_ptr = mysql_use_result(&my_connection);
if (res_ptr) {
while ((sqlrow = mysql_fetch_row(res_ptr))) {
if (first_row) {
display_header();
first_row = 0;
}
display_row();
}
if (mysql_errno(&my_connection)) {
printf("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;
}
void display_row()
{
unsigned int field_count;
field_count = 0;
while (field_count < mysql_field_count(&my_connection)) {
if (sqlrow[field_count]) {
printf("%s ", sqlrow[field_count]);
} else {
printf("NULL");
}
field_count++;
}
printf("\n");
}
void display_header()
{
MYSQL_FIELD *field_ptr;
printf("Column details:\n");
while ((field_ptr = mysql_fetch_field(res_ptr)) != NULL) {
printf("\t Name: %s\n", field_ptr->name);
printf("\t Type:");
if (IS_NUM(field_ptr->type)) {
printf("Numberic field\n");
} else {
switch (field_ptr->type) {
case FIELD_TYPE_VAR_STRING:
printf("VARCHAR\n");
break;
case FIELD_TYPE_LONG:
printf("LONG\n");
break;
default:
printf("Type is %d, check in mysql_num.h\n", field_ptr->type);
}
}
printf("\t Max width %ld\n", field_ptr->length);
if (field_ptr->flags & AUTO_INCREMENT_FLAG) {
printf("\t Auto increments\n");
}
printf("\n");
}
}