-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion_for_chap08mySQL
More file actions
186 lines (180 loc) · 7 KB
/
Question_for_chap08mySQL
File metadata and controls
186 lines (180 loc) · 7 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// page 352-353 of book Beginning Linux Programming , 4th edition 2007
//The book is using the program of page 350 as a base , and expands on it slightly
//to reach the following , at page 352-353 :
//Here are some definitions , taken from the book and not from official MySQL C API
//MYSQL *mysql_init(MYSQL *);
//MYSQL *mysql_real_connect(MYSQL *connection,const char *server_host,const char*sql_user_name,const char *sql_password,
const char *db_name,unsigned int port_number,const char *unix_socket_name,unsigned int flags);
//void mysql_close(MYSQL *connection);
//MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
//unsigned int mysql_errno(MYSQL *connection);
//char *mysql_error(MYSQL *connection);
//MYSQL_RES *mysql_use_result(MYSQL *connection);
//unsigned int mysql_field_count(MYSQL *connection);
//int mysql_query(MYSQL *connection, const char *query);
// VERSION 1 , according to book
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
void display_row();
MYSQL my_connection; // use of 2 global variables , so they are easily accessed from function display()
MYSQL_ROW sqlrow;
int main(void)
{
MYSQL_RES *res_ptr;
int res;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection,"localhost","rick","secret","practicedb", 0, NULL, 0))
{
printf("Connection success\n");
res = mysql_query(&my_connection,"SELECT childno, fname, age FROM children WHERE fname = 'Ann' ");
if (res) { printf("SELECT error %d: %s\n",mysql_errno(&my_connection), mysql_error(&my_connection)); }
else
{
res_ptr = mysql_use_result(&my_connection);
if (res_ptr)
{
while ((sqlrow = mysql_fetch_row(res_ptr)))
{
printf("Fetched data , one row at a time ...\n");
display_row();
}
if (mysql_errno(&my_connection))
{ fprintf(stderr, "Retrive error %d: %s\n",mysql_errno(&my_connection), 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() //no need for parameters list
{
unsigned int field_count; /* number of columns */
field_count = 0;
while ( field_count < mysql_field_count(&my_connection) ) //global var my_connection is visible in here
{
printf("%s ", sqlrow[field_count]); //global var sqlrow is visible in here
field_count++;
}
printf("\n");
}
// gcc -I/usr/include/mysql select3globvar.c -L/usr/lib/mysql -lmysqlclient -o select3globvar
// I implemented a variation of the above program , avoiding the local variables declaration
// so I had to modify the definition and declaration of display( ...)
//VERSION 2 , my alternate version , passing a pointer and a variable to the function
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
void display_row(MYSQL * some_connection_ptr , MYSQL_ROW dbrow);
int main(void)
{
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
int res;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection,"localhost","rick","secret","practicedb", 0, NULL, 0))
{
printf("Connection success\n");
res = mysql_query(&my_connection,"SELECT childno, fname, age FROM children WHERE fname = 'Ann' ");
if (res) { printf("SELECT error %d: %s\n",mysql_errno(&my_connection), mysql_error(&my_connection)); }
else
{
res_ptr = mysql_use_result(&my_connection);
if (res_ptr)
{
while ((sqlrow = mysql_fetch_row(res_ptr)))
{
printf("Fetched data , one row at a time ...\n");
display_row(&my_connection , sqlrow );
}
if (mysql_errno(&my_connection))
{ fprintf(stderr, "Retrive error %d: %s\n",mysql_errno(&my_connection), 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(MYSQL * some_connection_ptr , MYSQL_ROW dbrow)
{
unsigned int field_count; /* number of columns */
field_count = 0;
while ( field_count < mysql_field_count(some_connection_ptr) )
{
printf("%s ", dbrow[field_count]);
field_count++;
}
printf("\n");
}
// gcc -I/usr/include/mysql select3.c -L/usr/lib/mysql -lmysqlclient -o select3
// This is another implementation with alternate display(...) definition and declaration
// that does not uses pointers . Is it worse than the previous ?
// VERSION 3
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
void display_row(MYSQL some_connection , MYSQL_ROW dbrow);
int main(void)
{
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
int res;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection,"localhost","rick","secret","practicedb", 0, NULL, 0))
{
printf("Connection success\n");
res = mysql_query(&my_connection,"SELECT childno, fname, age FROM children WHERE fname = 'Ann' ");
if (res) { printf("SELECT error %d: %s\n",mysql_errno(&my_connection), mysql_error(&my_connection)); }
else
{
res_ptr = mysql_use_result(&my_connection);
if (res_ptr)
{
while ((sqlrow = mysql_fetch_row(res_ptr)))
{
printf("Fetched data , one row at a time ...\n");
display_row(my_connection , sqlrow );
}
if (mysql_errno(&my_connection))
{ fprintf(stderr, "Retrive error %d: %s\n",mysql_errno(&my_connection), 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(MYSQL some_connection , MYSQL_ROW dbrow)
{
unsigned int field_count; /* number of columns */
field_count = 0;
while ( field_count < mysql_field_count(&some_connection) )
{
printf("%s ", dbrow[field_count]);
field_count++;
}
printf("\n");
}
// gcc -I/usr/include/mysql select3.c -L/usr/lib/mysql -lmysqlclient -o select3