#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlca.h>
void connect();
void sql_err();void cursor();
void main()
{
EXEC SQL WHENEVER SQLERROR DO sql_err();
connect();
cursor();
EXEC SQL COMMIT RELEASE;
}
void sql_err()
{
printf("%.*s\n",sqlca.sqlerrm.sqlerrml,,sqlca.sqlerrm.sqlerrmc);
}
void connect()/*数据库连接-本地数据库,不添加server*/
{
EXEC SQL BEGIN DECLARE SECTION;
char username[20],password[20];
EXEC SQL END DECLARE SECTION;
printf("input username: ");
gets(username);
printf("input password: ");
gets(password);
EXEC SQL CONNECT :username IDENTIFIED BY :password;
}
void cursor()/*游标的标准使用方式,声明,打开,抓取,关闭*/
{
{
EXEC SQL BEGIN DECLARE SECTION;
int eno;
char name[20];
float salary;
char action;
EXEC SQL END DECLARE SECTION;
EXEC SQL DECLARE emp_cursor SCROLL CURSOR FOR
SELECT empno,ename,sal FROM emp ;
EXEC SQL OPEN emp_cursor;
EXEC SQL WHENEVER NOT FOUND DO sql_err();
for(;;)
{
printf("F:first,P:prior,C:current,L:last, ");
printf("N:next row,X:exit the app: ");
scanf("%c",&action);
getchar();
switch(action)
{
case 'f':
case 'F':
EXEC SQL FETCH FIRST emp_cursor INTO :eno,:name,:salary;
break;
case 'n':
case 'N':
EXEC SQL FETCH NEXT emp_cursor INTO :eno,:name,:salary;
break;
case 'p':
case 'P':
EXEC SQL FETCH PRIOR emp_cursor INTO :eno,:name,:salary;
break;
case 'L':
case 'l':
EXEC SQL FETCH LAST emp_cursor INTO :eno,:name,:salary;
break;
case 'c':
case 'C':
EXEC SQL FETCH CURRENT emp_cursor INTO :eno,:name,:salary;
break;
case 'x':
case 'X':
EXEC SQL CLOSE emp_cursor;
return;
}
printf("\n empno: %d,name: %s,salary:%4.0f\n",eno,name,salary);
}
}