Skip to content

Commit c61bcb3

Browse files
authored
Add files via upload
1 parent b08072f commit c61bcb3

File tree

1 file changed

+112
-33
lines changed

1 file changed

+112
-33
lines changed

library.c

Lines changed: 112 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void deletebook(void);
1818
void searchprice(void);
1919
void searchquantity(void);
2020
void searchrack(void);
21-
void advanc_search_byname(void);
21+
void advance_search_byname(void);
2222
void generic_search(char *);
2323
void issue_books(void);
2424

@@ -65,9 +65,9 @@ int main(int argc, char **argv)
6565
show_error(con);
6666

6767
char username[20],password[20];
68-
printf("Enter UserName : ");
68+
printf("\nEnter UserName : ");
6969
scanf("%s",username);
70-
printf("\nEnter Password : ");
70+
printf("Enter Password : ");
7171
scanf("%s",password);
7272

7373
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)
101101
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));"))
102102
show_error(con);
103103

104-
while(1)
105-
{
104+
105+
106106
printf("\n\nWhat do you want to do : \n");
107107
printf("1). Add Users\n");
108108
printf("2). Add Books Details \n");
@@ -114,8 +114,9 @@ void database_connect(void)
114114
printf("8). Exit\n");
115115
printf("\nEnter a Choice : ");
116116

117-
118-
switch(getchar())
117+
int c = getchar();
118+
c=getchar();
119+
switch(c)
119120
{
120121
case '1':
121122
addusers();
@@ -152,7 +153,7 @@ void database_connect(void)
152153
printf("\nPlease Enter Correct Input\n");
153154
break;
154155
}
155-
}
156+
156157
}
157158

158159
void addusers(void)
@@ -164,18 +165,28 @@ void addusers(void)
164165
scanf("%s",password);
165166
printf("Enter Full Name : ");
166167
scanf("%s",fullname);
167-
//users[] = "user";
168-
printf("%s",users);
169168

170169
snprintf(query, MAX_STRING, "INSERT INTO users(username,password,fullname,registered_date,role) VALUES ('%s','%s','%s',curdate(),'user')" , username,password,fullname);
171170

172171
if(mysql_query(con, query))
173172
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+
}
174185
}
175186

176187
void addbooks(void)
177188
{
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");
179190

180191
printf("Name : ");
181192
scanf("%s",a.name);
@@ -198,7 +209,6 @@ void addbooks(void)
198209
//printf("entered one is %d\n", a.rack_no);
199210

200211
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);
202212

203213
if(mysql_query(con, query))
204214
show_error(con);
@@ -211,6 +221,9 @@ void addbooks(void)
211221
{
212222
addbooks();
213223
}
224+
else{
225+
database_connect();
226+
}
214227
}
215228

216229
void fetchdata()
@@ -228,9 +241,10 @@ void fetchdata()
228241

229242
MYSQL_RES *result = mysql_store_result(con);
230243

231-
if (result == NULL)
244+
if (result->row_count == 0)
232245
{
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();
234248
}
235249

236250
int num_fields = mysql_num_fields(result);
@@ -255,8 +269,16 @@ void fetchdata()
255269
}
256270

257271
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+
}
260282
}
261283

262284
void issue_books(void)
@@ -275,7 +297,7 @@ void searchbooks()
275297
fprintf(stderr, "mysql_init() failed\n");
276298
exit(1);
277299
}
278-
printf("1). Search using Name\n");
300+
printf("\n1). Search using Name\n");
279301
printf("2). Advanced Search\n");
280302
printf("Enter your Choice : ");
281303
int choose=0;
@@ -328,7 +350,7 @@ void advancedsearch()
328350
case 5:
329351
searchrack();
330352
case 6:
331-
advanc_search_byname();
353+
advance_search_byname();
332354
case 7:
333355
exit(0);
334356
default:
@@ -341,7 +363,7 @@ void advancedsearch()
341363
void searchname()
342364
{
343365

344-
printf("Enter Book Name : ");
366+
printf("\nEnter Book Name : ");
345367
char name[100];
346368
scanf("%s",name);
347369

@@ -360,25 +382,45 @@ void searchname()
360382

361383
int num_fields = mysql_num_fields(result);
362384

385+
MYSQL_FIELD *field;
386+
387+
while((field = mysql_fetch_field(result)))
388+
{
389+
printf("%-14s", field->name);
390+
}
391+
printf("\n");
392+
363393
MYSQL_ROW row;
364394

365395
while ((row = mysql_fetch_row(result)))
366396
{
367397
for(int i = 0; i <= num_fields; i++)
368398
{
369-
printf("%s ", row[i]);
399+
printf("%-14s ", row[i]);
370400
}
371401
printf("\n");
372402
}
373403

374404
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+
375417
mysql_close(con);
376418
exit(0);
377419

378420
}
379421

380422

381-
void advanc_search_byname(void)
423+
void advance_search_byname(void)
382424
{
383425
printf("Enter Book Name : ");
384426
char name[100];
@@ -464,18 +506,38 @@ void generic_search(char * query)
464506

465507
int num_fields = mysql_num_fields(result);
466508

509+
MYSQL_FIELD *field;
510+
511+
while((field = mysql_fetch_field(result)))
512+
{
513+
printf("%-14s", field->name);
514+
}
515+
printf("\n");
516+
467517
MYSQL_ROW row;
468-
518+
469519
while ((row = mysql_fetch_row(result)))
470520
{
471521
for(int i = 0; i <= num_fields; i++)
472522
{
473-
printf("%s ", row[i]);
523+
printf("%-14s ", row[i]);
474524
}
475525
printf("\n");
476526
}
477527

478528
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+
}
479541
mysql_close(con);
480542
exit(0);
481543

@@ -499,6 +561,7 @@ void editbooks(void)
499561
if (result->row_count == 0)
500562
{
501563
fprintf(stdout, "%s\n", "There is no book in the library matching the entered ID.");
564+
database_connect();
502565
}
503566

504567
int num_fields = mysql_num_fields(result);
@@ -507,18 +570,16 @@ void editbooks(void)
507570

508571
while((field = mysql_fetch_field(result)))
509572
{
510-
printf("%20s", field->name);
573+
printf("%-14s", field->name);
511574
}
512575
printf("\n");
513576
MYSQL_ROW row;
514577

515-
516-
517578
while ((row = mysql_fetch_row(result)))
518579
{
519580
for(int i = 0; i <= num_fields; i++)
520581
{
521-
printf("%20s", row[i]);
582+
printf("%-14s ", row[i]);
522583
}
523584
printf("\n");
524585
}
@@ -528,31 +589,37 @@ void editbooks(void)
528589

529590
printf("Name : ");
530591
scanf("%s",a.name);
531-
//printf("entered one is %s\n", a.name);
532592

533593
printf("Author : ");
534594
scanf("%s",a.author);
535-
//printf("entered one is %s\n", a.author);
536595

537596
printf("Quantity : ");
538597
scanf("%d",&a.quantity);
539-
//printf("entered one is %d\n", a.quantity);
540598

541599
printf("Price : ");
542600
scanf("%d",&a.price);
543-
//printf("entered one is %d\n", a.price);
544601

545602
printf("Rack No : ");
546603
scanf("%d",&a.rack_no);
547-
//printf("entered one is %d\n", a.rack_no);
548604

549605
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");
551607

552608
if(mysql_query(con, query))
553609
show_error(con);
554610

555611
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+
}
556623
mysql_close(con);
557624
exit(0);
558625
}
@@ -569,4 +636,16 @@ void deletebook()
569636
if(mysql_query(con, query))
570637
show_error(con);
571638

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+
572651
}

0 commit comments

Comments
 (0)