modified to accept hostname and connect to the network
[csql.git] / examples / sqlapi / sqlapiexample.c
blob07135529829057e14b1342ad65dc1d6f5c458ffc
1 #include<SqlStatement.h>
2 int main()
4 DbRetVal rv = OK;
5 SqlConnection *con = new SqlConnection();
6 rv = con->connect("root", "manager");
7 if (rv != OK) return 1;
8 SqlStatement *stmt = new SqlStatement();
9 stmt->setSqlConnection(con);
10 char statement[1024];
11 strcpy(statement, "CREATE TABLE t1 (f1 int, f2 char(20), primary key (f1));");
12 int rows =0;
13 rv = stmt->prepare(statement);
14 if (rv != OK) {delete stmt; delete con; return -1; }
15 rv = stmt->execute(rows);
16 if (rv != OK) {delete stmt; delete con; return -1; }
17 stmt->free();
18 printf("Table t1 created\n");
20 strcpy(statement, "INSERT INTO t1 (f1, f2) VALUES (?, ?);");
21 int id1 =10;
22 char name[20];
23 strcpy(name, "Rithish");
24 rv = stmt->prepare(statement);
25 if (rv != OK) {delete stmt; delete con; return -1; }
26 int count =0;
27 for (int i = 0 ; i < 100 ; i++)
29 rv = con->beginTrans();
30 if (rv != OK) break;
31 id1 = i;
32 stmt->setIntParam(1, id1);
33 sprintf(name, "Gopika_%d", id1);
34 stmt->setStringParam(2, name);
35 rv = stmt->execute(rows);
36 if (rv != OK) break;
37 rv = con->commit();
38 if (rv != OK) break;
39 count++;
41 printf("Total Rows Inserted %d\n", count);
42 stmt->free();
44 strcpy(statement, "SELECT * FROM t1 where f1 = ?;");
45 rv = stmt->prepare(statement);
46 if (rv != OK) {delete stmt; delete con; return -1; }
47 stmt->bindField(1, &id1);
48 stmt->bindField(2, name);
49 for (int i = 0 ; i < 10 ; i++)
51 rv = con->beginTrans();
52 if (rv != OK) break;
53 stmt->setIntParam(1, i);
54 stmt->execute(rows);
55 while (stmt->fetch() != NULL) {
56 printf("Row value is %d %s\n", id1, name);
58 stmt->close();
59 rv = con->commit();
60 if (rv != OK) break;
62 stmt->free();
65 strcpy(statement, "DROP TABLE t1);");
66 rv = stmt->prepare(statement);
67 if (rv != OK) {delete stmt; delete con; return -1; }
68 rv = stmt->execute(rows);
69 if (rv != OK) {delete stmt; delete con; return -1; }
70 stmt->free();
71 printf("Table dropped\n");
74 delete stmt;
75 delete con;
76 return 0;