adding cacheloader library
[csql.git] / examples / dbapi / manDBAPIdelete.c
blob9e52b2a5dac195ddf205ff82d9568ac1602d37d3
1 // DBAPI Demo: delete and read
3 #include<CSql.h>
4 int main()
6 // Connect to the database
7 Connection conn;
8 DbRetVal rv = conn.open("root", "manager");
9 if (rv != OK) return 1;
11 // get database manager
12 DatabaseManager *dbMgr = conn.getDatabaseManager();
13 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
15 // open table
16 Table *table = dbMgr->openTable("EMP");
17 if (table == NULL)
19 printf("Unable to open table\n");
20 dbMgr->dropTable("EMP");
21 conn.close();
22 return 3;
25 // allocate fields to bind
26 int id1 = 0;
27 float sal=0.0;
28 char name[20];
29 int icount =0, i = 0;
31 // bind the allocated fields
32 table->bindFld("empId", &id1);
33 table->bindFld("name", name);
34 table->bindFld("salary", &sal);
36 conn.startTransaction();
37 table->setCondition(NULL);
38 rv = table->execute();
39 if (rv != OK)
41 dbMgr->closeTable(table);
42 dbMgr->dropTable("EMP");
43 conn.close();
44 return 4;
46 printf("Scan before deletion\n");
47 printf("********************\n");
48 printf("EmpId | name\t| salary\n");
49 printf("--------------------------\n");
51 void *fld2ptr, *tuple;
52 while(true)
54 tuple = (char*)table->fetch() ;
55 if (tuple == NULL) {break;}
56 printf("%d | %s\t| %6.2f\n", id1, name, sal);
58 conn.commit();
59 rv=table->closeScan();
61 Condition p1;
62 int val1 = 1005;
63 p1.setTerm("empId",OpGreaterThan, &val1);
64 table->setCondition(&p1);
65 conn.startTransaction();
66 rv=table->execute();
67 if (rv != OK)
69 dbMgr->closeTable(table);
70 dbMgr->dropTable("EMP");
71 conn.close();
72 return 5;
75 while(true)
77 tuple = (char*)table->fetch() ;
78 if (tuple == NULL) { break; }
79 table->deleteTuple();
81 conn.commit();
82 table->closeScan();
84 conn.startTransaction();
85 table->setCondition(NULL);
86 rv = table->execute();
87 if (rv != OK)
89 dbMgr->closeTable(table);
90 dbMgr->dropTable("EMP");
91 conn.close();
92 return 4;
94 printf("Scan after deletion\n");
95 printf("********************\n");
96 printf("EmpId | name\t| salary\n");
97 printf("--------------------------\n");
99 while(true)
101 tuple = (char*)table->fetch() ;
102 if (tuple == NULL) {break;}
103 printf("%d | %s\t| %6.2f\n", id1, name, sal);
105 conn.commit();
106 rv=table->closeScan();
107 dbMgr->closeTable(table);
108 dbMgr->dropTable("EMP");
109 conn.close();
110 return 0;