#include <stdio.h> #include <sqlca.h> #include <sqlda.h> #include <sqltypes.h> #include <sqlstype.h> #define SUCCESS 0 main() { EXEC SQL INCLUDE SQLCA; //包含sqlca EXEC SQL BEGIN DECLARE SECTION; //定义主变量 char *string1; char str1[10]; char str2[10]; char str3[10]; EXEC SQL END DECLARE SECTION; EXEC SQL CREATE DATABASE example3; //创建数据库 EXEC SQL DATABASE example3; //选择数据库
if (SQLCODE == SUCCESS) { EXEC SQL CREATE TABLE Student ( alarm char(6) , sname char(10), sex char(10) ); } EXEC SQL INSERT INTO Student VALUES("aaaa","bbbb","1"); //向数据库中插入数据 EXEC SQL INSERT INTO Student VALUES("dddd","dddd","2"); EXEC SQL INSERT INTO Student VALUES("cccc","cccc","3"); EXEC SQL //设置游标其中DECLARE是将查询得结果赋予fet_cur DECLARE fet_cur CURSOR FOR SELECT alarm, sname,sex FROM Student; EXEC SQL OPEN fet_cur; printf("SQLCODE=%d ", SQLCODE);
for(;;) { memset(str1, 0, sizeof(str1)); memset(str2, 0, sizeof(str2)); memset(str3, 0, sizeof(str3)); EXEC SQL FETCH fet_cur INTO :str1,:str2,:str3; /*把记录游标fet_cir移到一个新位置,凭且把记录值赋予变量str1和str2*/ if (SQLCODE != SUCCESS) { printf("LINE=%d, SQLCODE=%d", __LINE__, SQLCODE); break; } printf("str1=%s, str2=%s,str2=%s ",str1,str2,str3); } EXEC SQL CLOSE fet_cur; EXEC SQL CLOSE DATABASE; return(0); }