@@ -18,7 +18,7 @@ void deletebook(void);
18
18
void searchprice (void );
19
19
void searchquantity (void );
20
20
void searchrack (void );
21
- void advanc_search_byname (void );
21
+ void advance_search_byname (void );
22
22
void generic_search (char * );
23
23
void issue_books (void );
24
24
@@ -65,9 +65,9 @@ int main(int argc, char **argv)
65
65
show_error (con );
66
66
67
67
char username [20 ],password [20 ];
68
- printf ("Enter UserName : " );
68
+ printf ("\nEnter UserName : " );
69
69
scanf ("%s" ,username );
70
- printf ("\nEnter Password : " );
70
+ printf ("Enter Password : " );
71
71
scanf ("%s" ,password );
72
72
73
73
snprintf (query , MAX_STRING , "SELECT username,password FROM library.users WHERE username = '%s' AND password = '%s'" , username , password );
@@ -101,8 +101,8 @@ void database_connect(void)
101
101
if (mysql_query (con , "CREATE TABLE IF NOT EXISTS books(ID int(8) AUTO_INCREMENT,Name char(30),Author char(30),Quantity int(5),Price int(5), Rack_no int(5), PRIMARY KEY(ID));" ))
102
102
show_error (con );
103
103
104
- while ( 1 )
105
- {
104
+
105
+
106
106
printf ("\n\nWhat do you want to do : \n" );
107
107
printf ("1). Add Users\n" );
108
108
printf ("2). Add Books Details \n" );
@@ -114,8 +114,9 @@ void database_connect(void)
114
114
printf ("8). Exit\n" );
115
115
printf ("\nEnter a Choice : " );
116
116
117
-
118
- switch (getchar ())
117
+ int c = getchar ();
118
+ c = getchar ();
119
+ switch (c )
119
120
{
120
121
case '1' :
121
122
addusers ();
@@ -152,7 +153,7 @@ void database_connect(void)
152
153
printf ("\nPlease Enter Correct Input\n" );
153
154
break ;
154
155
}
155
- }
156
+
156
157
}
157
158
158
159
void addusers (void )
@@ -164,18 +165,28 @@ void addusers(void)
164
165
scanf ("%s" ,password );
165
166
printf ("Enter Full Name : " );
166
167
scanf ("%s" ,fullname );
167
- //users[] = "user";
168
- printf ("%s" ,users );
169
168
170
169
snprintf (query , MAX_STRING , "INSERT INTO users(username,password,fullname,registered_date,role) VALUES ('%s','%s','%s',curdate(),'user')" , username ,password ,fullname );
171
170
172
171
if (mysql_query (con , query ))
173
172
show_error (con );
173
+
174
+ printf ("\nDo you want to Add Another User : " );
175
+ scanf (" %c" , & choice );
176
+
177
+ if (choice == 'y' || choice == 'Y' )
178
+ {
179
+ addusers ();
180
+ }
181
+ else
182
+ {
183
+ database_connect ();
184
+ }
174
185
}
175
186
176
187
void addbooks (void )
177
188
{
178
- printf ("Insert values(Name,Author,Quantity,Price,Rack no.) into books.\n" );
189
+ printf ("\nInsert values(Name,Author,Quantity,Price,Rack no.) into books.\n" );
179
190
180
191
printf ("Name : " );
181
192
scanf ("%s" ,a .name );
@@ -198,7 +209,6 @@ void addbooks(void)
198
209
//printf("entered one is %d\n", a.rack_no);
199
210
200
211
snprintf (query , MAX_STRING , "INSERT INTO books(Name, Author, Quantity, Price, Rack_no) VALUES ('%s', '%s', %d, %d, %d)" , a .name , a .author , a .quantity , a .price , a .rack_no );
201
- printf ("Query made is : %s\n" , query );
202
212
203
213
if (mysql_query (con , query ))
204
214
show_error (con );
@@ -211,6 +221,9 @@ void addbooks(void)
211
221
{
212
222
addbooks ();
213
223
}
224
+ else {
225
+ database_connect ();
226
+ }
214
227
}
215
228
216
229
void fetchdata ()
@@ -228,9 +241,10 @@ void fetchdata()
228
241
229
242
MYSQL_RES * result = mysql_store_result (con );
230
243
231
- if (result == NULL )
244
+ if (result -> row_count == 0 )
232
245
{
233
- fprintf (stdout , "%s\n" , "Currently there are no books in the library" );
246
+ fprintf (stdout , "%s\n" , "There is currently no book in the library " );
247
+ database_connect ();
234
248
}
235
249
236
250
int num_fields = mysql_num_fields (result );
@@ -255,8 +269,16 @@ void fetchdata()
255
269
}
256
270
257
271
mysql_free_result (result );
258
- mysql_close (con );
259
- exit (0 );
272
+ printf ("\nDo you want to go to Other Options(Y/N)" );
273
+ scanf (" %c" , & choice );
274
+
275
+ if (choice == 'y' || choice == 'Y' )
276
+ {
277
+ database_connect ();
278
+ }
279
+ else {
280
+ exit (0 );
281
+ }
260
282
}
261
283
262
284
void issue_books (void )
@@ -275,7 +297,7 @@ void searchbooks()
275
297
fprintf (stderr , "mysql_init() failed\n" );
276
298
exit (1 );
277
299
}
278
- printf ("1 ). Search using Name\n" );
300
+ printf ("\n1 ). Search using Name\n" );
279
301
printf ("2). Advanced Search\n" );
280
302
printf ("Enter your Choice : " );
281
303
int choose = 0 ;
@@ -328,7 +350,7 @@ void advancedsearch()
328
350
case 5 :
329
351
searchrack ();
330
352
case 6 :
331
- advanc_search_byname ();
353
+ advance_search_byname ();
332
354
case 7 :
333
355
exit (0 );
334
356
default :
@@ -341,7 +363,7 @@ void advancedsearch()
341
363
void searchname ()
342
364
{
343
365
344
- printf ("Enter Book Name : " );
366
+ printf ("\nEnter Book Name : " );
345
367
char name [100 ];
346
368
scanf ("%s" ,name );
347
369
@@ -360,25 +382,45 @@ void searchname()
360
382
361
383
int num_fields = mysql_num_fields (result );
362
384
385
+ MYSQL_FIELD * field ;
386
+
387
+ while ((field = mysql_fetch_field (result )))
388
+ {
389
+ printf ("%-14s" , field -> name );
390
+ }
391
+ printf ("\n" );
392
+
363
393
MYSQL_ROW row ;
364
394
365
395
while ((row = mysql_fetch_row (result )))
366
396
{
367
397
for (int i = 0 ; i <= num_fields ; i ++ )
368
398
{
369
- printf ("%s " , row [i ]);
399
+ printf ("%-14s " , row [i ]);
370
400
}
371
401
printf ("\n" );
372
402
}
373
403
374
404
mysql_free_result (result );
405
+
406
+ printf ("\nDo you want to go to Other Options(Y/N)" );
407
+ scanf (" %c" , & choice );
408
+
409
+ if (choice == 'y' || choice == 'Y' )
410
+ {
411
+ database_connect ();
412
+ }
413
+ else {
414
+ exit (0 );
415
+ }
416
+
375
417
mysql_close (con );
376
418
exit (0 );
377
419
378
420
}
379
421
380
422
381
- void advanc_search_byname (void )
423
+ void advance_search_byname (void )
382
424
{
383
425
printf ("Enter Book Name : " );
384
426
char name [100 ];
@@ -464,18 +506,38 @@ void generic_search(char * query)
464
506
465
507
int num_fields = mysql_num_fields (result );
466
508
509
+ MYSQL_FIELD * field ;
510
+
511
+ while ((field = mysql_fetch_field (result )))
512
+ {
513
+ printf ("%-14s" , field -> name );
514
+ }
515
+ printf ("\n" );
516
+
467
517
MYSQL_ROW row ;
468
-
518
+
469
519
while ((row = mysql_fetch_row (result )))
470
520
{
471
521
for (int i = 0 ; i <= num_fields ; i ++ )
472
522
{
473
- printf ("%s " , row [i ]);
523
+ printf ("%-14s " , row [i ]);
474
524
}
475
525
printf ("\n" );
476
526
}
477
527
478
528
mysql_free_result (result );
529
+
530
+ printf ("\nDo you want to go to Other Options(Y/N)" );
531
+ scanf (" %c" , & choice );
532
+
533
+ if (choice == 'y' || choice == 'Y' )
534
+ {
535
+ database_connect ();
536
+ }
537
+ else
538
+ {
539
+ exit (0 );
540
+ }
479
541
mysql_close (con );
480
542
exit (0 );
481
543
@@ -499,6 +561,7 @@ void editbooks(void)
499
561
if (result -> row_count == 0 )
500
562
{
501
563
fprintf (stdout , "%s\n" , "There is no book in the library matching the entered ID." );
564
+ database_connect ();
502
565
}
503
566
504
567
int num_fields = mysql_num_fields (result );
@@ -507,18 +570,16 @@ void editbooks(void)
507
570
508
571
while ((field = mysql_fetch_field (result )))
509
572
{
510
- printf ("%20s " , field -> name );
573
+ printf ("%-14s " , field -> name );
511
574
}
512
575
printf ("\n" );
513
576
MYSQL_ROW row ;
514
577
515
-
516
-
517
578
while ((row = mysql_fetch_row (result )))
518
579
{
519
580
for (int i = 0 ; i <= num_fields ; i ++ )
520
581
{
521
- printf ("%20s " , row [i ]);
582
+ printf ("%-14s " , row [i ]);
522
583
}
523
584
printf ("\n" );
524
585
}
@@ -528,31 +589,37 @@ void editbooks(void)
528
589
529
590
printf ("Name : " );
530
591
scanf ("%s" ,a .name );
531
- //printf("entered one is %s\n", a.name);
532
592
533
593
printf ("Author : " );
534
594
scanf ("%s" ,a .author );
535
- //printf("entered one is %s\n", a.author);
536
595
537
596
printf ("Quantity : " );
538
597
scanf ("%d" ,& a .quantity );
539
- //printf("entered one is %d\n", a.quantity);
540
598
541
599
printf ("Price : " );
542
600
scanf ("%d" ,& a .price );
543
- //printf("entered one is %d\n", a.price);
544
601
545
602
printf ("Rack No : " );
546
603
scanf ("%d" ,& a .rack_no );
547
- //printf("entered one is %d\n", a.rack_no);
548
604
549
605
snprintf (query , MAX_STRING , "UPDATE books SET Name = '%s', Author = '%s', Quantity = %d, Price = %d, Rack_no = %d WHERE ID = %d" , a .name , a .author , a .quantity , a .price , a .rack_no ,id );
550
- printf ("Record Successfully Updated" );
606
+ printf ("\nRecord Successfully Updated" );
551
607
552
608
if (mysql_query (con , query ))
553
609
show_error (con );
554
610
555
611
mysql_free_result (result );
612
+ printf ("\nDo you want to go to Other Options(Y/N)" );
613
+ scanf (" %c" , & choice );
614
+
615
+ if (choice == 'y' || choice == 'Y' )
616
+ {
617
+ database_connect ();
618
+ }
619
+ else
620
+ {
621
+ exit (0 );
622
+ }
556
623
mysql_close (con );
557
624
exit (0 );
558
625
}
@@ -569,4 +636,16 @@ void deletebook()
569
636
if (mysql_query (con , query ))
570
637
show_error (con );
571
638
639
+ printf ("\nDo you want to go to Other Options(Y/N)" );
640
+ scanf (" %c" , & choice );
641
+
642
+ if (choice == 'y' || choice == 'Y' )
643
+ {
644
+ database_connect ();
645
+ }
646
+ else
647
+ {
648
+ exit (0 );
649
+ }
650
+
572
651
}
0 commit comments