PRO*C连接到未知的Oracle数据库示例

slhcrj9b  于 2023-01-16  发布在  Oracle
关注(0)|答案(1)|浏览(98)

我无法从PRO*c代码连接到我的数据库示例orclpdb1。我尝试进行调试,但它显示了一些不存在的其他数据库名称。

void Svc_Login() {
    
        varchar orauid[ USER_ID_SIZE ];
        varchar oraupw[ PASSWORD_SIZE ];
        int table_count=0;
        char curr_schema[20];
        char curr_db[20];
        char str_user_id[20];
        char str_user_pw[20];
    
        /*TDT,II - 02 May 2006 - Added Level 1 Debugging */
        if ( iDebug >= 1 ) WriteTrace( "Running Svc_Login" );
    
        orauid.len = sprintf( orauid.arr, "item" );
        oraupw.len = sprintf( oraupw.arr, "item" );
    
        EXEC SQL CONNECT :orauid IDENTIFIED BY :oraupw;  
       
        EXEC SQL select user into :curr_schema from dual;
    
          sprintf( cLogText, "Connecting with username %s and password %s",VARCHAR2CHAR(str_user_id, orauid),VARCHAR2CHAR(str_user_pw,oraupw);
          WriteTrace(cLogText);
    
        EXEC SQL select ora_database_name into :curr_db from dual;
        sprintf( cLogText, "The current db is %s", curr_db );
        WriteTrace(cLogText);
    
        EXEC SQL select count(*) into :table_count from tab;
        sprintf( cLogText, "The No.of Tables in %s schema is ~%d~", curr_schema,table_count );
        WriteTrace(cLogText);
    
    
    
        return;             /* return to mainline code */
    
    }

#define VARCHAR2CHAR(a,b)    (strncpy(a, (char*)b.arr, b.len), a[b.len] = '\0')

以下是环境规范脚本。

#!/bin/ksh

# Set Oracle environment up first
# Set Path and Oracle environment
VERSION=`uname -v`
RELEASE=`uname -r`
            export ORACLE_HOME=/opt/oracle/product/19c/dbhome_1
            export ORACLE_SID=orclpdb1
            export PATH=$PATH:$ORACLE_HOME/bin
            export WAUUSER=item
            export WAUPASSW=item
            export ITEMUSER=item
            export ITEMPASSW=item

以下是调试语句的日志,表明它的用户ID和密码为空值。并且SID名称也不同(32c)。SID应为orclpdb1。

oracle@azureRHEL MenuPrograms]$ cd /opt/cao/logs/
[oracle@azureRHEL logs]$ cat caoforms.trc.log
04:40:19: Setting Debug Level to ~2~
04:40:19: Running Svc_Login
04:40:21: Connecting with username (null) and password (null)
04:40:21: The current db is 32c
04:40:21: The No.of Tables in k schema is ~0~
04:40:29: Running SecurityEnterKey
04:40:29: orLoginInfo.user_id_count value is ~0~
04:40:29: MAHESH
04:40:29:
04:40:31: Running wrap_up
[oracle@azureRHEL logs]$
3pvhb19x

3pvhb19x1#

执行SQL连接:用户标识符:passw,使用:dbstr;语法起作用。未添加早期的USING。现在它工作正常。应在主机变量的帮助下以:/SID格式提供连接字符串

#include <stdio.h>

EXEC SQL INCLUDE SQLCA.H;
EXEC SQL INCLUDE ORACA.H;

void check_err() {
    if (sqlca.sqlcode < 0) {
        printf("Error: %s\n", sqlca.sqlerrm.sqlerrmc);
        exit(1);
    }
}

int main() {
    // Connect to the database
        char uid[10]="item";
        char upw[10]="item";
        char dbsid[10]="orclpdb1";
        char connect_string[50]="4.246.55.119:1521/orclpdb1";

    EXEC SQL CONNECT :uid IDENTIFIED BY :upw USING :connect_string;
    check_err();

    // Get the number of tables
    int num_tables;
    EXEC SQL SELECT count(*) INTO :num_tables FROM tab;
    check_err();

    // Print the result
    printf("Number of tables: %d\n", num_tables);

    // Disconnect from the database

    check_err();

    return 0;
}

相关问题