1
[csql.git] / examples / sqlapi / sqlapiexample.c
blobeebe6fb7147ef3599bda8687229592a8d1671983
1 /***************************************************************************
2 * *
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
4 * *
5 ***************************************************************************/
7 #include<SqlFactory.h>
8 #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;
15 AbsSqlStatement *stmt = SqlFactory::createStatement(CSql);
16 stmt->setConnection(con);
17 char statement[1024];
18 strcpy(statement, "CREATE TABLE t1 (f1 int, f2 char(20), primary key (f1));");
19 int rows =0;
20 rv = stmt->prepare(statement);
21 if (rv != OK) {delete stmt; delete con; return -1; }
22 rv = stmt->execute(rows);
23 if (rv != OK) {delete stmt; delete con; return -1; }
24 stmt->free();
25 printf("Table t1 created\n");
27 strcpy(statement, "INSERT INTO t1 (f1, f2) VALUES (?, ?);");
28 int id1 =10;
29 char name[20];
30 strcpy(name, "Rithish");
31 rv = stmt->prepare(statement);
32 if (rv != OK) {delete stmt; delete con; return -1; }
33 int count =0;
34 for (int i = 0 ; i < 100 ; i++)
36 rv = con->beginTrans();
37 if (rv != OK) break;
38 id1 = i;
39 stmt->setIntParam(1, id1);
40 sprintf(name, "Gopika_%d", id1);
41 stmt->setStringParam(2, name);
42 rv = stmt->execute(rows);
43 if (rv != OK) break;
44 rv = con->commit();
45 if (rv != OK) break;
46 count++;
48 printf("Total Rows Inserted %d\n", count);
49 stmt->free();
51 strcpy(statement, "SELECT * FROM t1 where f1 = ?;");
52 rv = stmt->prepare(statement);
53 if (rv != OK) {delete stmt; delete con; return -1; }
54 stmt->bindField(1, &id1);
55 stmt->bindField(2, name);
56 for (int i = 0 ; i < 10 ; i++)
58 rv = con->beginTrans();
59 if (rv != OK) break;
60 stmt->setIntParam(1, i);
61 stmt->execute(rows);
62 while (stmt->fetch() != NULL) {
63 printf("Row value is %d %s\n", id1, name);
65 stmt->close();
66 rv = con->commit();
67 if (rv != OK) break;
69 stmt->free();
72 strcpy(statement, "DROP TABLE t1;");
73 rv = stmt->prepare(statement);
74 if (rv != OK) {delete stmt; delete con; return -1; }
75 rv = stmt->execute(rows);
76 if (rv != OK) {delete stmt; delete con; return -1; }
77 stmt->free();
78 printf("Table dropped\n");
80 delete stmt;
81 delete con;
82 return 0;