- 论坛徽章:
- 0
|
本帖最后由 happer_xc 于 2013-03-10 16:28 编辑
程序如下:
fun1.pc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
EXEC SQL include sqlca;
#ifndef SQLCODE
#define SQLCODE sqlca.sqlcode
#endif
int fun1()
{
EXEC SQL BEGIN DECLARE SECTION;
char bb[9];
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT bb INTO :bb FROM tab1 WHERE aa='1';
if( SQLCODE ) {
printf("[%s-%d] [%d]\n",__FILE__,__LINE__,SQLCODE);
exit (-1);
}
printf("In fun1 bb:[%s]\n",bb);
return 0;
}
编译:
proc fun1.pc
cc -G -o libmytest.so fun1.c -L$ORACLE_HOME/lib -lclntsh
main.pc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
EXEC SQL include sqlca;
#ifndef SQLCODE
#define SQLCODE sqlca.sqlcode
#endif
int main()
{
EXEC SQL BEGIN DECLARE SECTION;
char *user = "u";
char *pass = "p";
char bb[9];
EXEC SQL END DECLARE SECTION;
void *handle;
int (*func)();
EXEC SQL CONNECT :user IDENTIFIED BY :pass;
if( SQLCODE ) {
printf("[%s-%d] [%d]\n",__FILE__,__LINE__,SQLCODE);
exit (-1);
}
EXEC SQL SELECT bb INTO :bb FROM tab1 WHERE aa='1';
if( SQLCODE ) {
printf("[%s-%d] [%d]\n",__FILE__,__LINE__,SQLCODE);
exit (-1);
}
printf("In main bb:[%s]\n",bb);
if( (handle=dlopen("./libmytest.so",RTLD_NOW)) == NULL ) {
printf("打开动态库失败!\n");
exit (-1);
}
if( (func = (int (*)())dlsym(handle,"fun1")) == NULL ) {
printf("打开函数失败!\n");
exit (-1);
}
func();
dlclose(handle);
EXEC SQL ROLLBACK WORK RELEASE;
exit (0);
}
编译:
proc main.pc
cc -g -o a main.c -L$ORACLE_HOME/lib -lclntsh
执行结果:
In main bb:[22]
[fun1.c-308] [-1012]
oerr ora 1012
01012, 00000, "not logged on"
// *Cause:
// *Action:
在fun1中再connect一次数据库就能取出数据!
可是我在其他数据库编译的时候都不用再次打开数据库,不知道是proc这种情况在编译的时候需要加特殊的参数吗还是有别的什么方式。请指教。
谢谢 |
|