-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_tamo_connect.c
575 lines (482 loc) · 17.1 KB
/
mysql_tamo_connect.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*********************************************/
/* Developer:-Tamoghna Bhaduri */
/* Official Mail:- [email protected] */
/* Tester:- Debabrata Mondal */
/* Distributio restricted */
/********************************************/
#include "my_project.h"
#include <mysql.h>
/***************************************/
MYSQL *conn,*conn1;
MYSQL_RES *res; /*Macro Declaration*/
MYSQL_ROW row;
MYSQL * conect_db();
/**************************************/
//char api_token[MAX_LIMIT]="5eccdeea0341a3.51273563"; /* HatiRamPati*/
/****************Connection Checker***********************/
MYSQL * connect_db()
{
char *server = "localhost";
char *user = "root";
char *password = "Sayanti@123#"; /* set me first */
char *database = "Port_Folio";
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server, user,password, database, 0, NULL, 0))
{
printf("\n\n ** Unable to connect. Please check password for sql data base also check the database **\n\n");
printf("\n** Check the file called mysql_tamo_connect.c \n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
return conn;
}
/**********************************************************/
void show(char *, MYSQL *);
void delete_table(char *,MYSQL *);
int main()
{
int num=0,size,i=0,sow=0;
FILE *fptr;
char *Share_Name,*temp,*temp1,show_help1[MAX_LIMIT]="select * from ",show_help2[MAX_LIMIT]=" Where Opening_Price is not null";
printf("\n\n/////////********* Please seect these options with utmost care **************\n\n");
printf("\n Press any key to continue::\n");
getchar();
system("clear");
printf("Press 0 to create Port_Folio\n\n\n");
printf("\n\n ** Please dont give 1. 1 is strictly for Tamoghna's laptop ** \n\n");
printf("Enter 2 to modify the table and then see it\n\n\n");
printf("Enter 3 for getting stock update and creating table\n\n");
scanf("%d",&num);
switch(num)
{
case 0:
printf("\n\nPress any key::\n\n");
getchar();
create_Port_Folio();
system("clear");
printf("\n Press 0 to see the temporary Port_Folio::\n");
scanf("%d",&sow);
printf("\n\nPress 1 to make this Port_Folio parmanent:: and thenshow it\n\n");
if(sow==1)
{
conn=connect_db();
if(mysql_query(conn,"create table My_Port_Folio SELECT * FROM Debu_Port_Folio;insert My_Port_Folio select * from Debu_Port_Folio"))
{
printf("\n Some error\n");
fprintf(stderr, "%s\n", mysql_error(conn));
}
else
{
system("clear");
printf("\n\nThanks for using our program\n\n");
}
}
else
{
system("clear");
printf("\n\n *** Good Bye my fellow human Being ****\n\n");
}
if(sow==0)
{
show_portfolio();
}
break;
case 1:
printf("\n\n This option only works in Tamoghna's Laptop. Machine Bind. This will be removed. Dont Try this. Well please update table::\n\n");
update_table_tamo();// this shows my database
show_table_tamo();// this creates html and show in browser
break;
default:
printf("\nPlease stick to option given before:: \n");
break;
case 3:
printf("\n\n #### #### Press Any Key to Continue #### ####\n\n");
getchar();
Share_Name=get_details();//this should be inside the libtamo.so
printf("\n** %s **\n",Share_Name);
if(fptr=fopen("Temp.csv","r"))
{
printf("\nFile is seen\n");
fseek(fptr,0,SEEK_END);
size=ftell(fptr);
if(size!= 0)
{
printf("\nYou cleared the first hurdle, first step towards the DB creation is done::%d\n",size);
}
else
{
printf("\n Some error happpened. Check the token, check internet connection. Check if you have proper package::\n");
}
}
else
{
printf("\n\n It seem NSE firewall is closed or you are having error with token\n\n Also you may have to use different token\n\n");
}
conn=connect_db();
delete_table(Share_Name,conn);
temp=create_table_from_share(Share_Name);
//conn=connect_db();
if (mysql_query(conn,temp))
{
printf("\n Table cant be created. Please check create_table_from_share function\n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
temp1=load_data(Share_Name);
if (mysql_query(conn, temp1))
{
printf("\n\nProblem with loading into database. Please check the stored media \n\n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
free(temp);
temp=(char *)malloc(strlen(show_help1)+strlen(Share_Name)+strlen(show_help2)+1);
strcpy(temp,show_help1);
strncat(temp,Share_Name,strlen(Share_Name));
strncat(temp,show_help2,strlen(show_help2));
conn1=connect_db();
if (mysql_query(conn1,temp))
{
printf("Error");
}
show(Share_Name, conn1);
}
/* mysql_free_result(res);
mysql_close(conn);
mysql_close(conn1);*/
return 0;
}
/********************************************************************/
/********************************************************************/
void delete_table(char *Share_Name,MYSQL *conn1)
{
char t[MAX_LIMIT]="drop table if exists ",*tmp;
tmp=(char*)malloc(strlen(t)+1+strlen(Share_Name));
//strcpy(tmp,t);
snprintf(tmp,strlen(t),"%s",t);
strncat(tmp,Share_Name,strlen(Share_Name));
system("clear");
printf("\n This is to drop tha table :: %s::\n",tmp);
conn1=connect_db();
if ( mysql_query(conn1,tmp))
{
printf("\nCheck delete-table function\n");
}
else
{
printf("\nExisting database deleted\n");
}
free(tmp);
mysql_close(conn1);
}
/********************************************************************/
/********************************************************************/
char * load_data(char *st)
{
char *t1,*t2;
t1=strtok(st,"\n");
t2=(char *) malloc(strlen(load1)+strlen(st)+strlen(load2)+10);
strcat(t2,load1);
strcat(t2,t1);
strcat(t2,load2);
//printf("\n\n load data == %s \n\n",t2);
return t2;
}
/********************************************************************/
char * create_table_from_share(char *st)
{
char *t1,*t2;
t1=strtok(st,"\n");
t2=(char *) malloc(strlen(create1)+strlen(st)+strlen(create2)+1);
//strcpy(t2,create1);
snprintf(t2,strlen(create1),"%s",create1);
strcat(t2,t1);
strcat(t2,create2);
//printf("\n\n &&&& %s \n\n",t2);
return t2;
}
/********************************************************************/
/*****************************************************************************************************************************************************/
/******************************************************** This is the basic part for updating or inserting already ceated table. Not to be called ****/
/****************************************************************************************************************************************************/
void update_table_tamo()
{
printf("\n\n You choose to creae database manually. Thats something for Offline user. Brace yourself. Please do not enter wrong entry\n");
printf("\nYou will be given chance to update any entry later.\n");
printf("\nPlease confirm how many entry you want to perform this time\n");
scanf("%d",&num_of_share);
//fflush(stdin);
getchar();
for(i=1;i<=num_of_share;i++)
{
printf("\n Please provide your %dth share name ::\n",i);
fgets(Share_Name,MAX_LIMIT,stdin);
printf("\nEnter the CMP of %s ::",Share_Name);
fgets(cmp,MAX_LIMIT,stdin);
printf("\n %s \n %s",Share_Name,cmp);
tmp=add_inv_comma(Share_Name);//These functions are inside the libtamo.so
cmp1=update_query(tmp,cmp);//Happy Dynamic Linking
printf("\n %s \n",cmp1);
conn=connect_db();
if (mysql_query(conn, cmp1))
{
printf("\nPlease check your share name:: Try Again\n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
}
mysql_close(conn);
}
/***********************************************************************************************************************************/
/************************ This is the function to create user defined Port_Folio **************************************************/
/*********************************************************************************************************************************/
void create_Port_Folio()
{
conn=connect_db();
char *t1,*t2,*t3,Share_Name[MAX_LIMIT],Buying_Price[MAX_LIMIT];
int i,num;
char cmp[MAX_LIMIT],amount[MAX_LIMIT];
system("clear");
printf("\n Please confirm How many Stock data you want to enter in the port folio::\n");
scanf("%d",&num);
if(!(mysql_query(conn,"drop table if exists Debu_Port_Folio")))
{
printf("\\n**** So you are first time user!! ****\n\n");
printf("\n** Press any key to continue**\n");
getchar();
system("clear");
if(mysql_query(conn,"create table Debu_Port_Folio(Name_of_Share varchar(50),Current_Market_Price Decimal(10,2),Buying_Price Decimal(10,2), Amount_Bought INT(20))"))
{
printf("Unable to create the table itself.Looks like you already have existin port_folio in our database");
fprintf(stderr, "%s\n", mysql_error(conn));
}
printf("\n\n *** Congrats Empty Port_Folio Created in Database ***\n\n Lets Enter data now ***\n\n");
for(i=0;i<num;i++)
{
printf("\nPlease enter the name of Share, better if you enter the symbo::\n");
fgets(Share_Name,MAX_LIMIT,stdin);
printf("\nPlease enter the current marcket price\n");
fgets(cmp,MAX_LIMIT,stdin);
printf("\nPlease enter the Buying Price::\n");
fgets(Buying_Price,MAX_LIMIT,stdin);
printf("\n Please enter the amount of share bought::\n");
fgets(amount,MAX_LIMIT,stdin);
t1=value_insert_query(Share_Name,cmp,Buying_Price,amount);
if(mysql_query(conn,t1))
{
printf("\n\n*** Some problem while inserting the values in table. Please do check the functions***\n\n\n");
printf("%s",t1);
fprintf(stderr, "%s\n", mysql_error(conn));
}
}
}
}
/********************************************************************************************************************************/
/************************** Show table in HTML page and open it in Firefox browser *********************************************/
/********************************************************************************************************************************/
/******************************************************************************************************/
/******************************************************************************************************/
/********* This function simply show the table. Not to be called *************************************/
void show_table_tamo()
{
int i=0;
system("rm -rf *.html");
system("clear");
fptr=fopen("temp.html","w+");
fputs(t1,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t11,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t12,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t2,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t3,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t4,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t5,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t6,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t61,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t7,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
conn1=connect_db();
if (mysql_query(conn1,"select Name_of_Share, Current_Marcket_Price,Buying_Price, Amount_Bought from Kite"))
{
printf("\nPlease check your share name:: Show Result\n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn1);
while ((row = mysql_fetch_row(res)) != NULL)
{
fputs(table_open,fptr);
fputs(row[0],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[1],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[2],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[3],fptr);
fputs(table_close,fptr);
fprintf(fptr,"\n");
}
fprintf(fptr,"\n");
fputs(t8,fptr);
fclose(fptr);
system("firefox temp.html &");
}
/***************************************************************************************************************************************************************/
/**************************************************************************************************************************************************************/
void show_portfolio()
{
int i=0;
system("rm -rf *.html");
system("clear");
fptr=fopen("temp.html","w+");
fputs(t1,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t11,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t12,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t2,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t3,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t4,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t5,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t6,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t61,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t7,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
conn1=connect_db();
if (mysql_query(conn1,"select Name_of_Share, Current_Market_Price,Buying_Price, Amount_Bought from Debu_Port_Folio"))
{
printf("\nPlease check your share name:: Show Result\n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn1);
while ((row = mysql_fetch_row(res)) != NULL)
{
fputs(table_open,fptr);
fputs(row[0],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[1],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[2],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[3],fptr);
fputs(table_close,fptr);
fprintf(fptr,"\n");
}
fprintf(fptr,"\n");
fputs(t8,fptr);
fclose(fptr);
system("firefox temp.html &");
}
void show(char *t, MYSQL *conn1)
{
int i=0;
char *tmp;
tmp=(char *)malloc(strlen(t1)+1);
//printf("\n Killing Firefox. It will open but take time. \n");
//printf("\n inside the show function::%s\n",t1);
system("rm -rf *.html");
system("clear");
fptr=fopen("temp.html","w+");
fputs(t1,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t11,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t12,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t2,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t3,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t4,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t5,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t6,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(t61,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
fputs(sh,fptr);
fprintf(fptr,"\n");
fprintf(fptr,"\t");
res = mysql_use_result(conn1);
while ((row = mysql_fetch_row(res)) != NULL)
{
fputs(table_open,fptr);
fputs(row[0],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[1],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[2],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[3],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[4],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[5],fptr);
fputs(table_data_close,fptr);
fputs(table_data_open,fptr);
fputs(row[6],fptr);
fputs(table_close,fptr);
fprintf(fptr,"\n");
}
fprintf(fptr,"\n");
fputs(t8,fptr);
fclose(fptr);
system("firefox temp.html &");
}