1 // DBAPI Demo: insert and read
7 // Connect to the database
9 DbRetVal rv
= conn
.open("root", "manager");
10 if (rv
!= OK
) return 1;
12 // get database manager
13 DatabaseManager
*dbMgr
= conn
.getDatabaseManager();
14 if (dbMgr
== NULL
) { printf("Auth failed\n"); return 2;}
18 tabDef
.addField("empId", typeInt
, 0, NULL
, true);
19 tabDef
.addField("name", typeString
, 20);
20 tabDef
.addField("salary", typeFloat
);
23 rv
= dbMgr
->createTable("EMP", tabDef
);
24 if (rv
!= OK
) { printf("Table creation failed\n"); conn
.close(); return 3; }
25 printf("Table created\n");
26 HashIndexInitInfo
*idxInfo
= new HashIndexInitInfo();
27 strcpy(idxInfo
->tableName
, "EMP");
28 idxInfo
->list
.append("empId");
29 idxInfo
->isUnique
= true;
30 idxInfo
->isPrimary
= true;
31 idxInfo
->indType
= hashIndex
;
32 rv
= dbMgr
->createIndex("indx1", idxInfo
);
33 if (rv
!= OK
) { printf("Index creation failed\n"); return ErrUnknown
; }
36 Table
*table
= dbMgr
->openTable("EMP");
39 printf("Unable to open table\n");
40 dbMgr
->dropTable("EMP");
45 // allocate fields to bind
47 char *setNames
[10] = {"Praba", "Kishor", "Jiten", "Gopal", "Aruna", "Ravi", "Kiran" , "Sanjay", "Rajesh", "Arun" };
52 // bind the allocated fields
53 table
->bindFld("empId", &id1
);
54 table
->bindFld("name", name
);
55 table
->bindFld("salary", &sal
);
58 conn
.startTransaction();
59 for (i
= 0 ; i
< 10; i
++)
61 // set values to insert into table
63 sal
= 1000.00 * (i
+ 1);
64 strcpy(name
, setNames
[i
]);
67 rv
= table
->insertTuple();
72 printf("%d Tuples inserted\n", i
);
75 conn
.startTransaction();
77 // set condition to select all the tuples
78 table
->setCondition(NULL
);
79 rv
= table
->execute();
82 dbMgr
->closeTable(table
);
83 dbMgr
->dropTable("EMP");
86 printf("\ninserted values are as follows\n");
87 printf("EmpId | name\t| salary\n");
88 printf("--------------------------\n");
92 // fetch each tuple satisfying the condition
93 char * tuple
= (char*)table
->fetch();
94 if (tuple
== NULL
) {break;}
95 printf("%d | %s\t| %6.2f\n", id1
, name
, sal
);
98 // commit the transaction
101 //close the condition
104 // close the table using the table handle
105 dbMgr
->closeTable(table
);
107 //close the connection