1 /*One connection working with multiple statements
2 *create table T1 with two fields,
3 *One connection working with multiple statements simultaneously for csql,
5 *create 3 stmts for inserting into 5 tables and set the same connection
6 *insert into all 3 tables and commit the transaction
8 *select the records from 3 tables in same connection
10 *Author : Nihar Paital
18 AbsSqlConnection
*con
= createConnection();
19 rv
= con
->connect("root","manager");
21 AbsSqlStatement
*stmt1
= createStatement();
22 AbsSqlStatement
*stmt2
= createStatement();
23 AbsSqlStatement
*stmt3
= createStatement();
26 stmt1
->setConnection(con
);
27 stmt2
->setConnection(con
);
28 stmt3
->setConnection(con
);
33 // insert records into T1
34 if(strcmp(getenv("DSN"),"oracle")==0) {
35 f1varT1
= malloc(sizeof(long long));
36 f1varT2
= malloc(sizeof(long long));
37 f1varT3
= malloc(sizeof(long long));
39 f1varT1
= malloc(sizeof(int));
40 f1varT2
= malloc(sizeof(int));
41 f1varT3
= malloc(sizeof(int));
43 char f2varT1
[36] = "lakshya";
44 printf("Inserting records into table T1\n");
45 strcpy(statement
,"INSERT INTO T1 VALUES(?,?);");
46 rv
= stmt1
->prepare(statement
);
47 if(rv
!=OK
) { delete stmt1
; delete con
; return 3; }
49 rv
= con
->beginTrans();
50 for(int j
=0;j
<5;j
++) {
51 if(strcmp(getenv("DSN"),"oracle")==0) {
52 *(long long *) f1varT1
= j
;
53 stmt1
->setLongLongParam(1,*(long long *)f1varT1
);
56 stmt1
->setIntParam(1,*(int *) f1varT1
);
58 stmt1
->setStringParam(2,f2varT1
);
59 rv
= stmt1
->execute(rows
);
63 printf("%d rows inserted into table T1\n",count
);
65 // insert records into T2
66 char f2varT2
[36] = "lakshya";
67 printf("Inserting records into table T2\n");
68 strcpy(statement
,"INSERT INTO T2 VALUES(?,?);");
69 rv
= stmt2
->prepare(statement
);
70 if(rv
!=OK
) { delete stmt2
; delete con
; return 3; }
72 for(int j
=5;j
<10;j
++) {
73 if(strcmp(getenv("DSN"),"oracle")==0) {
74 *(long long *) f1varT2
= j
;
75 stmt2
->setLongLongParam(1,*(long long *)f1varT2
);
78 stmt2
->setIntParam(1,*(int *) f1varT2
);
80 stmt2
->setStringParam(2,f2varT2
);
81 rv
= stmt2
->execute(rows
);
85 printf("%d rows inserted into table T2\n",count
);
87 // insert records into T3
88 char f2varT3
[36] = "lakshya";
89 printf("Inserting records into table T3\n");
90 strcpy(statement
,"INSERT INTO T3 VALUES(?,?);");
91 rv
= stmt3
->prepare(statement
);
92 if(rv
!=OK
) { delete stmt3
; delete con
; return 3; }
94 for(int j
=10;j
<15;j
++) {
95 if(strcmp(getenv("DSN"),"oracle")==0) {
96 *(long long *) f1varT3
= j
;
97 stmt3
->setLongLongParam(1,*(long long *)f1varT3
);
100 stmt3
->setIntParam(1,*(int *) f1varT3
);
102 stmt3
->setStringParam(2,f2varT3
);
103 rv
= stmt3
->execute(rows
);
107 printf("%d rows inserted into table T3\n",count
);
110 free(f1varT1
); free(f1varT2
); free(f1varT3
);
111 stmt1
->free(); delete stmt1
;
112 stmt2
->free(); delete stmt2
;
113 stmt3
->free(); delete stmt3
;