使用C++和Pro C/C++对Oracle进行操作

lxf_1976 2003-04-17 05:55:19
入门问题!

目的是为了用C++进行对Oracle数据库的操作,目前我的思路是使用Pro C/C++完成处理数据库数据的模块,C++调用该Pro C/C++模块的接口完成应用层!但不知道如何实现!

请求高手举个最简单的例子,包括C++和Pro C/C++的原码及编译文件!

或者是否可以提供另一种思路及实现方法(最简单的例子)!
...全文
86 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
black_snail 2003-04-23
  • 打赏
  • 举报
回复
ORACLE8i Example:
/*
* sample2.pc
*
* This program connects to ORACLE, declares and opens a cursor,
* fetches the names, salaries, and commissions of all
* salespeople, displays the results, then closes the cursor.
*/

#include <stdio.h>
#include <string.h>
#include <sqlca.h>
#include <stdlib.h>
#include <sqlda.h>
#include <sqlcpr.h>

#define UNAME_LEN 20
#define PWD_LEN 8

/*
* Use the precompiler typedef'ing capability to create
* null-terminated strings for the authentication host
* variables. (This isn't really necessary--plain char *'s
* would work as well. This is just for illustration.)
*/
typedef char asciiz[PWD_LEN];
typedef char asciie[5];

EXEC SQL TYPE asciiz IS CHARZ(PWD_LEN) REFERENCE;
EXEC SQL TYPE asciie IS CHARZ(5) REFERENCE;
asciiz username;
asciiz password;

struct emp_info
{
asciie emp_name;
float salary;
float commission;
};

void sql_error(msg)
char *msg;
{
char err_msg[512];
size_t buf_len, msg_len;

EXEC SQL WHENEVER SQLERROR CONTINUE;

printf("\n%s\n", msg);

/* Call sqlglm() to get the complete text of the
* error message.
*/
buf_len = sizeof (err_msg);
sqlglm(err_msg, &buf_len, &msg_len);
printf("%.*s\n", msg_len, err_msg);

EXEC SQL ROLLBACK RELEASE;
exit(EXIT_FAILURE);
}

void main()
{
struct emp_info *emp_rec_ptr;

/* Allocate memory for emp_info struct. */
if ((emp_rec_ptr =
(struct emp_info *) malloc(sizeof(struct emp_info))) == 0)
{
fprintf(stderr, "Memory allocation error.\n");
exit(EXIT_FAILURE);
}

/* Connect to ORACLE. */
strcpy(username, "SCOTT");
strcpy(password, "TIGER");

EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--");

EXEC SQL CONNECT :username IDENTIFIED BY :password;
printf("\nConnected to ORACLE as user: %s\n", username);

/* Declare the cursor. All static SQL explicit cursors
* contain SELECT commands. 'salespeople' is a SQL identifier,
* not a (C) host variable.
*/
EXEC SQL DECLARE salespeople CURSOR FOR
SELECT RTRIM(ENAME), SAL, COMM
FROM EMP
WHERE JOB LIKE 'SALES%';

/* Open the cursor. */
EXEC SQL OPEN salespeople;

/* Get ready to print results. */
printf("\n\nThe company's salespeople are--\n\n");
printf("Salesperson Salary Commission\n");
printf("----------- ------ ----------\n");

/* Loop, fetching all salesperson's statistics.
* Cause the program to break the loop when no more
* data can be retrieved on the cursor.
*/
EXEC SQL WHENEVER NOT FOUND DO break;

for (;;)
{
EXEC SQL FETCH salespeople INTO :emp_rec_ptr;
printf("%s %9.2f %12.2f\n", emp_rec_ptr->emp_name,
emp_rec_ptr->salary, emp_rec_ptr->commission);
}

/* Close the cursor. */
EXEC SQL CLOSE salespeople;

printf("\nArrivederci.\n\n");

EXEC SQL COMMIT WORK RELEASE;
exit(EXIT_SUCCESS);
}
lxf_1976 2003-04-19
  • 打赏
  • 举报
回复
实在是惭愧,第一种思路最后我的症结就是在此,我试过将proc写的程序打成lib,但是C++的编译文件不知道如何写,才能将该lib编译进来!

请楼上的兄弟是否可以举个最简单的例子!(proc的小程序及编译文件、C++的小程序及编译文件,或者一个编译文件直接编译proc和C++)

谢谢!
Maybeyond 2003-04-18
  • 打赏
  • 举报
回复
我很想知道啊
有没有人知道阿
qfsb_p 2003-04-18
  • 打赏
  • 举报
回复
你把proc写的打成lib或者是dll不就可以了吗?
lxf_1976 2003-04-18
  • 打赏
  • 举报
回复
多谢2楼兄弟的答复!

目前我这里存在地问题是:使用Pro C/C++已经封装了操作数据的类,但不知道使用C++如何调用该类,说到底,最后可能是编译的问题,就是说如何将Pro C/C++的类联编到C++的程序中!

现在我正在看Oracle的OCI接口,不知道是否可以替换上面的思路!
jiezhi 2003-04-17
  • 打赏
  • 举报
回复
可以这么作:
先封装好的数据层的类,里面添加操作数据的方法:add(参数),delete(参数),
find(参数),update(参数)等
商业层/界面层调用类的方法,传入参数,实现业务逻辑和界面输入控制

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧