Posted on 2006-03-07 20:40
天涯浪子 阅读(658)
评论(0) 编辑 收藏 引用
3.下面从Pro*C的一个简单程序入手:
#include <iostream>
using namespace std;
EXEC SQL INCLUDE SQLCA;
EXEC SQL BEGIN DECLARE SECTION;
char username[30];
char password[20];
char last_name[30];
EXEC SQL END DECLARE SECTION;
void sqlerror();
main()
{
EXEC SQL WHENEVER SQLERROR
DO sqlerror();
strcpy(username,"langzi");
strcpy(password,"langzi");
EXEC SQL CONNECT:username
IDENTIFIED BY:password;
EXEC SQL select LAST_NAME
into:last_name from S_EMP where id=2;
printf("\nID=2,last_name=%s\n",last_name);
}
void sqlerror()
{
EXEC SQL WHENEVER SQLERROR
CONTINUE;
printf("\n=>oracle error detected:\n");
printf("%.60s\n",sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK RELEASE;
exit(1);
}
4.从上面的程序简要看一下其结构:
(1)include头文件(包括c/c++和Pro*C/C++)
(2)定义变量
(3)定义函数
(4)写主方法main
联接数据库:connect
SQL操作语句:EXEC SQL..........;
exception
断开联接,释放连接:EXEC SQL COMMIT/ROLLBACK WORK RELEASE