Removing large file for data load. Modified README to generate mysql and psql input...
[csql.git] / test / dbapi / Connection / conntest13.c
blob6438aa3c63178cc9428ad8c415f0a631db057187
1 //In a transaction mix insert, update and delete a tuple.
2 //All should take effect in a transaction
4 #include<CSql.h>
5 int main()
7 Connection conn;
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;}
12 TableDef tabDef;
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; }
20 int id = 0;
21 char name[196] = "PRABAKARAN";
22 table->bindFld("f1", &id);
23 table->bindFld("f2", name);
24 int i;
25 // int icount =0;
27 conn.startTransaction();
29 //Inserts 10 tuples into the table
30 for (i=0; i<10; i++)
32 id = i;
33 rv = table->insertTuple();
34 if (rv != OK)
36 printf("Insertion failed\n");
37 dbMgr->dropTable("t1");
38 conn.close();
39 return 5;
41 //icount++;
43 printf("%d tuples are inserted into the dababase\n", i);
45 //Updates the tuple having f1=5
46 Condition p1;
47 int val1 = 5;
48 p1.setTerm("f1",OpEquals, &val1);
49 table->setCondition(&p1);
50 rv=table->execute();
51 if (rv != OK)
53 dbMgr->closeTable(table);
54 dbMgr->dropTable("t1");
55 conn.close();
56 return 4;
58 void *tuple;
59 while(true)
61 tuple = (char*)table->fetch() ;
62 if (tuple == NULL) { break; }
63 strcpy(name,"Kanchana");
64 rv = table->updateTuple();
65 if (rv != OK)
67 printf("Error during updation");
68 dbMgr->closeTable(table);
69 dbMgr->dropTable("t1");
70 conn.close();
71 return 5;
74 table->close();
75 printf("Updated successfully\n");
77 //Delets the tuple having f1=8
78 table->setCondition(NULL);
79 val1 = 8;
80 p1.setTerm("f1",OpEquals, &val1);
81 table->setCondition(&p1);
82 rv=table->execute();
83 if (rv != OK)
85 dbMgr->closeTable(table);
86 dbMgr->dropTable("t1");
87 conn.close();
88 return 4;
91 while(true)
93 tuple = (char*)table->fetch() ;
94 if (tuple == NULL) { break; }
95 rv = table->deleteTuple();
96 if (rv != OK)
98 printf("Error during deletion");
99 dbMgr->closeTable(table);
100 dbMgr->dropTable("t1");
101 conn.close();
102 return 6;
105 table->close();
106 printf("Deleted successfully");
108 conn.commit();
110 conn.startTransaction();
111 //Displays the database after the transaction
112 table->setCondition(NULL);
113 table->execute();
114 while(true)
116 tuple = (char*)table->fetch() ;
117 if (tuple == NULL) {break;}
118 printf("\nBinded Tuple value is %d %s", id, name);
120 conn.commit();
122 table->close();
123 dbMgr->closeTable(table);
124 dbMgr->dropTable("t1");
125 conn.close();
126 return 0;