PRO*C开发入门

PRO*C开发入门,第1张

PRO*C开发入门 1.首先测试了proc是否可用
[xxxx@localhost ~]$ proc
proc: error while loading shared libraries: libclntsh.so.19.1: cannot open shared object file: No such file or directory

初步判断是动态链接的路径没关联,查看了$LD_LIBRARY_PATH 环境变量的值为空,则设置了环境变量的值为$ORALCE_HOME/lib(对应的目录),并(source 或 点号)生效

2.再次执行proc:提示命令用法
Pro*C/C++: Release 19.0.0.0.0 - Production on Fri Apr 1 22:57:59 2022
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.

System default option values taken from: /opt/oracle/product/19c/dbhome_1/precomp/admin/pcscfg.cfg

Option Name    Current Value  Description
-------------------------------------------------------------------------------
auto_connect   no             Allow automatic connection to ops$ account
char_map       charz          Mapping of character arrays and strings
close_on_commitno             Close all cursors on COMMIT
cmax           100            CMAX Value for connection pool
cmin           2              CMIN Value for connection pool
后续未粘贴
3.先检查当前linux用户可以使用oracle登录数据库,因为在pro*c程序中需要登录数据库。


sqlplus user/passwd@连接名称  (就是$ORACLE_HOME/network/admin/tnsnames.ora定义的连接名称)
tnsnames.ora 小部分示例
ORCLPDB =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCLPDB1)
    )

如tnsname.ora里面定义的是这样,连接名就是ORCLPDB
注意一下SERVICE_NAME为数据库的名称,在oracle12c后,引入了一种容器的概念,涉及到PDB(可插拔数据库),如果是PDB的oracle用户,则对应SERVICE_NAMRE为PDB的名称,使用select NAME from v$pdbs;查看
更详细的说明请见:redhat7 RPM 安装oracle 19c详细步骤和相关问题的解决方法
ORACLE 12C新特性:CDB与PDB详解

本人遇到问题:linux上的oracle用户可以登录数据库,但是linux上的其他用户不能登录数据库。



最后检查发现是其他用户的用户所属组的问题。



首先id oracle查看linux的oracle用户存在哪些分组。


然后将自己要用的用户也变更成对应的用户组,然后其他组作为附加组。



基本上将自己需要的用户如 zhangsan。


加上oracle用户目录所属组,以及oracle安装文件目录所属组 就基本可以了

先使用root用户,然后usermod -g 组1 zhangsan;(组1可以是oracle用户目录所属组)
                然后usermod -G 组2 zhangsan;(组2则为oracle安装文件目录所属组)
				然后使用zhangsan用户,sqlplus sys as sysdba 看能否登陆数据库
				然后sqlplus user/passwd@连接名称 能否使用数据库一般用户进行数据库登陆,如果都可以,则OK
4.编写pro*c源码
/*网上找的简单示例:*/
#include 
#include 
#include 
#include 

void sql_error();

struct info         /*结构体*/
{
    int id;
    char name[20];   
    char sex[6];
    char tel[12];
};

struct info_ind    /* 指示器变量*/
{
    short id_ind;
    short name_ind;
    short sex_ind;
    short tel_ind;
};

int main(int argc,char * argv[])
{

    char *pConn="user/passwd@ORCLPDB";
    int id=0;              /*宿主变量*/     
    char strname[20];      
    char strsex[4];
    char strtel[11];
    struct info myinfo;
    struct info_ind ind;

    EXEC SQL WHENEVER SQLERROR DO sql_error();
    /*连接数据库*/
    EXEC SQL CONNECT :pConn;
    if (sqlca.sqlcode == 0)
    printf("connect sucess\n");
    else
    printf("connect fail\n");
#if 0
    EXEC SQL drop table student;  /*如果有该表则先干掉,再创表*/
#endif
    EXEC SQL create table student(stuid int not null,stuname varchar(20) not null,stusex varchar(4),stutel char(11));      /*创表*/

    EXEC SQL insert into student values(1,'feng','boy','12589894789');  /*插入数据*/
    EXEC SQL insert into student values(2,'wang','girl','07245689145');
    EXEC SQL INSERT INTO student VALUES(3,'long','boy','13122514899');
    EXEC SQL insert into student values(4,'xiong','girl','13838383388');
    EXEC SQL insert into student values(5,'zhang','boy','15858568945');
    while (1)
    {
        printf("please input the id(0-exit):");
        fscanf(stdin,"%d",&id);
        if( 0 == id ){
            printf( "exit.\n" );
            return 0;
        }
       /*EXEC SQL select stuname,stusex,stutel into :strname ,:strsex,:strtel from student where stuid=:id;   */   /*可以单个插*/
        EXEC SQL select stuid,stuname,stusex,stutel into :myinfo indicator :ind from student where stuid =:id;  /*可以插入到一个结构体*/
    printf("\nthe infomation:%s\n",myinfo.name);
    printf("%s\n",myinfo.sex);
    printf("%s\n",myinfo.tel);
    }
    EXEC SQL commit work release;      /*提交*/
    return 0;
}

void sql_error()
{
    printf("oracle error\n");
    char buf[100];
    int buflen,msglen=0;
    EXEC SQL WHENEVER SQLERROR CONTINUE;
    EXEC SQL ROLLBACK WORK RELEASE;
    buflen=sizeof(buf);
    sqlglm(buf,&buflen,&msglen);
    printf("%d-%s\n",msglen,buf);
    exit(1);
}
5.编译,链接 生成可执行文件
#编写了个sh,执行编译过程
#修改成你的pro*c 文件
in=test.pc 
#修改成你要生成的c文件
out=test.c

proc iname=$in oname=$out

gcc -I${ORACLE_HOME}/precomp/public $out -o test -L${ORACLE_HOME}/lib -lclntsh
6.执行
./test

至此程序就跑起来了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/562895.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-02
下一篇 2022-04-02

发表评论

登录后才能评论

评论列表(0条)

保存