1 /* create table T1 with 10 fields.
2 * insert some records into it.
3 * select * from T1 with where clause;
6 #include<SqlNwConnection.h>
7 #include<SqlNwStatement.h>
13 AbsSqlConnection
*con
= new SqlNwConnection();
14 con
->setInnerConnection(NULL
);
15 SqlNwConnection
*conn
= (SqlNwConnection
*)con
;
16 conn
->setHost("localhost", 5678);
17 rv
= con
->connect("root","manager");
19 AbsSqlStatement
*stmt
= new SqlNwStatement();
20 stmt
->setInnerStatement(NULL
);
21 stmt
->setConnection(con
);
23 strcpy(statement
,"CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 INT,F10 BIGINT, F11 BINARY(4));");
26 rv
= stmt
->prepare(statement
);
27 if(rv
!=OK
) { delete stmt
; delete con
; return 1; }
29 rv
= stmt
->execute(rows
);
30 if(rv
!=OK
) { delete stmt
; delete con
; return 2; }
32 printf("Table created\n");
36 strcpy(statement
,"INSERT INTO T1 VALUES(?,?,?,?,?,?,?,?,?,?,?);");
40 char f3var
[31]="jitendra";
44 f6var
.set(2007,01,21);
50 f8var
.setDate(2007,01,21);
51 f8var
.setTime(12,29,30);
54 long long f10var
= 12000;
55 char f11var
[8]="23fe";
56 rv
= stmt
->prepare(statement
);
57 if(rv
!=OK
) { delete stmt
; delete con
; return 3; }
63 rv
= con
->beginTrans();
66 stmt
->setIntParam(1,f1var
);
67 stmt
->setShortParam(2,f2var
);
68 stmt
->setStringParam(3,f3var
);
69 stmt
->setFloatParam(4,f4var
);
70 stmt
->setFloatParam(5,f5var
);
71 stmt
->setDateParam(6,f6var
);
72 stmt
->setTimeParam(7,f7var
);
73 stmt
->setTimeStampParam(8,f8var
);
74 stmt
->setIntParam(9,f9var
);
75 stmt
->setLongLongParam(10,f10var
);
76 stmt
->setBinaryParam(11,f11var
);
77 rv
= stmt
->execute(rows
);
83 printf("Total rows inserted %d\n",count
);
86 //**********************************************************
89 strcpy(statement
,"SELECT * FROM T1 WHERE F1=? AND F2=? AND F3=? AND F4=? AND F5=? AND F6=? AND F7=? AND F8=? AND F9=? AND F10=? AND F11=?;");
90 rv
= stmt
->prepare(statement
);
91 if(rv
!=OK
) { delete stmt
; delete con
; return 4; }
92 stmt
->bindField(1,&f1var
);
93 stmt
->bindField(2,&f2var
);
94 stmt
->bindField(3,f3var
);
95 stmt
->bindField(4,&f4var
);
96 stmt
->bindField(5,&f5var
);
97 stmt
->bindField(6,&f6var
);
98 stmt
->bindField(7,&f7var
);
99 stmt
->bindField(8,&f8var
);
100 stmt
->bindField(9,&f9var
);
101 stmt
->bindField(10,&f10var
);
102 stmt
->bindField(11,f11var
);
105 char f3var1
[20]="jitendra";
109 f6var1
.set(2007,1,21);
112 f7var1
.set(12,29,30);
115 f8var1
.setDate(2007,1,21);
116 f8var1
.setTime(12,29,30);
119 long long f10var1
=12000;
120 char f11var1
[8]="23fe";
123 rv
= con
->beginTrans();
125 stmt
->setIntParam(1,f1var1
);
126 stmt
->setShortParam(2,f2var1
);
127 stmt
->setStringParam(3,f3var1
);
128 stmt
->setFloatParam(4,f4var1
);
129 stmt
->setFloatParam(5,f5var1
);
130 stmt
->setDateParam(6,f6var1
);
131 stmt
->setTimeParam(7,f7var1
);
132 stmt
->setTimeStampParam(8,f8var1
);
133 stmt
->setIntParam(9,f9var1
);
134 stmt
->setLongLongParam(10,f10var1
);
135 stmt
->setBinaryParam(11,f11var1
);
139 while(stmt
->fetch(rv
)!=NULL
)
141 printf("F1=%d | F2=%hd | F3=%s | F4=%f | F5=%f | DATE=%d-%d-%d | TIME=%d:%d:%d | TIMESTAMP=%d-%d-%d %d:%d:%d | F9=%d | F10=%lld",f1var
,f2var
,f3var
,f4var
,f5var
,f6var
.year(),f6var
.month(),f6var
.dayOfMonth(),f7var
.hours(),f7var
.minutes(),f7var
.seconds(),f8var
.year(),f8var
.month(),f8var
.dayOfMonth(),f8var
.hours(),f8var
.minutes(),f8var
.seconds(),f9var
,f10var
);
143 AllDataType::printVal(f11var
,typeBinary
,4);
152 printf("%d Tuples fetched\n",count
);
154 strcpy(statement
,"DROP TABLE T1;");
155 rv
= stmt
->prepare(statement
);
156 if(rv
!=OK
){delete stmt
; delete con
; return -1;}
158 rv
= stmt
->execute(rows
);
159 if(rv
!=OK
){delete stmt
;delete con
;return -1;}
161 printf("Table dropped\n");