modified to accept hostname and connect to the network
[csql.git] / examples / dbapi / manDBAPIupdate.c
blob7680d2e130ed7a3651aa0f5f0e3d8402c09f3722
1 // DBAPI Demo: update 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 updation\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->close();
61 Condition p1;
62 int val1 = 1006;
63 p1.setTerm("empId",OpLessThan, &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 if (id1 == 1001) {
80 strcpy(name,"Shubha");
81 sal = 1111.00;
82 table->updateTuple();
84 else if (id1 == 1003) {
85 strcpy(name,"Champak");
86 sal = 3333.00;
87 table->updateTuple();
89 else if (id1 == 1005) {
90 strcpy(name,"Poonam");
91 sal = 5555.00;
92 table->updateTuple();
95 conn.commit();
96 table->close();
98 conn.startTransaction();
99 table->setCondition(NULL);
100 rv = table->execute();
101 if (rv != OK)
103 dbMgr->closeTable(table);
104 dbMgr->dropTable("EMP");
105 conn.close();
106 return 4;
108 printf("Scan after updation\n");
109 printf("********************\n");
110 printf("EmpId | name\t| salary\n");
111 printf("--------------------------\n");
113 while(true)
115 tuple = (char*)table->fetch() ;
116 if (tuple == NULL) {break;}
117 printf("%d | %s\t| %6.2f\n", id1, name, sal);
119 conn.commit();
120 rv=table->close();
121 dbMgr->closeTable(table);
122 conn.close();
123 return 0;