*** empty log message ***
[csql.git] / examples / sqlapi / manSQLAPIupdate.c
blobd44f19bf35dd2686f400e0bb797bdb9b0b2f95d5
1 // open table EMP
2 // UPDATE WHERE EID = 1001, 1003, 1005
3 // seletct all rows from the table.
5 #include<SqlFactory.h>
6 #include <AbsSqlStatement.h>
9 int main()
11 DbRetVal rv = OK;
12 AbsSqlConnection *con = SqlFactory :: createConnection(CSql);
13 rv = con->connect("root","manager");
14 if(rv!=OK)return 1;
16 AbsSqlStatement *stmt = SqlFactory :: createStatement(CSql);
17 stmt->setConnection(con);
18 char statement[200];
20 int rows = 0;
22 // UPDATE EMP WITH WHERE CLAUSE(EID<1006)
23 strcpy(statement, "UPDATE EMP SET SALARY=?, ENAME=? WHERE EID=?;" );
24 rv = stmt->prepare(statement);
25 if(rv != OK) { delete stmt; delete con; return 3; }
27 int eid = 1001;
28 char ename[20];
29 float salary;
30 strcpy(ename, "Mani");
32 char *name[20] = { "Ravi", "Kiran", "Ganesh", "Yogesh", "Vishnu" };
34 int i = 0;
35 while(true) {
36 eid = 1001 + 2 * i;
37 if (eid > 1005) break;
38 strcpy(ename, name[i]);
39 salary = 1111.00 * (1 + i);
40 rv = con->beginTrans();
41 stmt->setFloatParam(1,salary);
42 stmt->setStringParam(2,ename);
43 stmt->setIntParam(3, eid);
44 if(rv != OK) { delete stmt; delete con; return 4; }
45 rv = stmt->execute(rows);
46 rv = con->commit();
47 if(rv != OK) { delete stmt; delete con; return 5; }
48 i++;
50 stmt->free();
52 //fetching the rows from EMP Table
53 strcpy(statement, "SELECT * FROM EMP;");
54 rv = stmt->prepare(statement);
55 if(rv != OK) { delete stmt; delete con; return 6; }
57 int count=0;
59 stmt->bindField(1, &eid);
60 stmt->bindField(2, ename);
61 stmt->bindField(3, &salary);
63 printf("updated values are as follows\n");
64 printf("EmpId | name\t| salary\n");
65 printf("--------------------------\n");
67 rv = con->beginTrans();
68 if(rv != OK) { delete stmt; delete con; return 6; }
69 stmt->execute(rows);
70 while(stmt->fetch() != NULL)
71 printf("%d | %s\t| %6.2f\n", eid, ename, salary);
72 rv = con->commit();
73 if(rv != OK) { delete stmt; delete con; return 7; }
74 stmt->close();
75 stmt->free();
76 delete stmt;
77 delete con;
78 return 0;