1 //In a transaction mix insert, update and delete a tuple.
2 //All should take effect in a transaction
8 DbRetVal rv
= conn
.open("root", "manager");
9 if (rv
!= OK
) { printf("Unable to open database\n"); return 1;}
10 DatabaseManager
*dbMgr
= conn
.getDatabaseManager();
11 if (dbMgr
== NULL
) { printf("Auth failed\n"); return 2;}
13 tabDef
.addField("f1", typeInt
);
14 tabDef
.addField("f2", typeString
, 196);
15 rv
= dbMgr
->createTable("t1", tabDef
);
16 if (rv
!= OK
) { printf("Table creation failed\n"); return 3; }
17 printf("Table created\n");
18 Table
*table
= dbMgr
->openTable("t1");
19 if (table
== NULL
) { printf("Unable to open table\n"); return 4; }
21 char name
[196] = "PRABAKARAN";
22 table
->bindFld("f1", &id
);
23 table
->bindFld("f2", name
);
27 conn
.startTransaction();
29 //Inserts 10 tuples into the table
33 rv
= table
->insertTuple();
36 printf("Insertion failed\n");
37 dbMgr
->dropTable("t1");
43 printf("%d tuples are inserted into the dababase\n", i
);
45 //Updates the tuple having f1=5
48 p1
.setTerm("f1",OpEquals
, &val1
);
49 table
->setCondition(&p1
);
53 dbMgr
->closeTable(table
);
54 dbMgr
->dropTable("t1");
61 tuple
= (char*)table
->fetch() ;
62 if (tuple
== NULL
) { break; }
63 strcpy(name
,"Kanchana");
64 rv
= table
->updateTuple();
67 printf("Error during updation");
68 dbMgr
->closeTable(table
);
69 dbMgr
->dropTable("t1");
75 printf("Updated successfully\n");
77 //Delets the tuple having f1=8
78 table
->setCondition(NULL
);
80 p1
.setTerm("f1",OpEquals
, &val1
);
81 table
->setCondition(&p1
);
85 dbMgr
->closeTable(table
);
86 dbMgr
->dropTable("t1");
93 tuple
= (char*)table
->fetch() ;
94 if (tuple
== NULL
) { break; }
95 rv
= table
->deleteTuple();
98 printf("Error during deletion");
99 dbMgr
->closeTable(table
);
100 dbMgr
->dropTable("t1");
106 printf("Deleted successfully");
110 conn
.startTransaction();
111 //Displays the database after the transaction
112 table
->setCondition(NULL
);
116 tuple
= (char*)table
->fetch() ;
117 if (tuple
== NULL
) {break;}
118 printf("\nBinded Tuple value is %d %s", id
, name
);
123 dbMgr
->closeTable(table
);
124 dbMgr
->dropTable("t1");