在informix里,我门要把一个记录里得值全部都读出来,然后在屏幕上把这些记录值给打印出来得话,我门该如何实现呢,那我们就要使用到游标得问题,下面是一段使用游标得简单代码,这是一个ec程序:
#include <stdio.h>
#include <sqlca.h>
#include <sqlda.h>
#include <sqltypes.h>
#include <sqlstype.h>
main()
{
EXEC SQL INCLUDE SQLCA; /*定义通信区*/
EXEC SQL BEGIN DECLARE SECTION; /*主变量定义开始*/
char *string1;
char str1[10];
char str2[10];
EXEC SQL END DECLARE SECTION; /*主变量定义结束*/
//EXEC SQL DROP DATABASE example;
EXEC SQL CREATE DATABASE example;
EXEC SQL DATABASE example; /*连接数据库*/
if (SQLCODE==0)
{
EXEC SQL CREATE TABLE Student
( alarm char(6) ,
sname char(10)
);
}
//插入数据
EXEC SQL INSERT INTO Student VALUES("aaaa","bbbb");
EXEC SQL INSERT INTO Student VALUES("dddd","dddd");
EXEC SQL INSERT INTO Student VALUES("cccc","cccc");
//设置游标其中DECLARE是将查询得结果赋予fet_cur
EXEC SQL DECLARE fet_cur CURSOR FOR
SELECT alarm, sname FROM Student;
EXEC SQL OPEN fet_cur;
printf("SQLCODE=%d\n", SQLCODE);
while(1)
{
memset(str1, 0, sizeof(str1));
memset(str2, 0, sizeof(str2));
//把记录游标fet_cir移到一个新位置,凭且把记录值赋予变量str1和str2
EXEC SQL FETCH fet_cur INTO :str1,:str2;
if (SQLCODE !=0)
{
printf("LINE=%d, SQLCODE=%d", __LINE__, SQLCODE);
break;
}
//打印记录结果
printf("str1=%s, str2=%s\n",str1,str2);
}
//关闭游标和数据库
EXEC SQL CLOSE fet_cur;
EXEC SQL CLOSE DATABASE;
/*
EXEC SQL SELECT first 1 alarm,sname
INTO :str1,:str2 FROM Student;
if (SQLCODE !=0)
{
printf("LINE=%d, SQLCODE=%d", __LINE__, SQLCODE);
return(-1);
}
printf("str1=%s, str2=%s\n",str1,str2);
*/
return(0);
}
其结果是在屏幕上打印出从informix里读出得数据:
str1=aaaa , str2=bbbb
str1=dddd , str2=dddd
str1=cccc , str2=cccc
希望这段小小代码能棒得上你得忙,我得QQ:29921374 欢迎交流
本文地址:http://com.8s8s.com/it/it26665.htm