標籤:
一、提要
上文簡單介紹了Windows下ProC配置開發,這次我們使用Linux平台再次配置Oracle ProC開
發環境(RedHat Linux 9 + Oracle 92)。
《ORACLE資料庫開發(一).Windows下配置使用ProC》和《ORACLE資料庫開發(二).Linux下配置使用ProC》
這兩篇文章的目的只是做一些基礎介紹,至於Oracle ProC編譯參數以及Linux下的ProC Makefile
相關內容,將再後續文章逐步引入。
一言以弊之,先易後難。
二、資料庫環境
與Windows下十分類似,首先確認安裝了組件,Oracle - Application Development -
Pro C-C++ 。安裝後會在$ORACLE_HOME/bin產生相應可執行檔,在$ORACLE_HOME/precomp/demo/proc
下也會產生一些makefile檔案和樣本。
三、樣本檔案
main.pc
---------------------------------------------------------
#include "sqlca.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void sql_error(char *msg)
{
printf("\n%s %s\n", msg,(char *)sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK RELEASE;
exit(0);
}
int main() {
EXEC SQL INCLUDE sqlca;
EXEC ORACLE OPTION (RELEASE_CURSOR = YES);
EXEC SQL WHENEVER SQLERROR DO sql_error(" <ERROR> ");
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR oraCN[30];
EXEC SQL END DECLARE SECTION;
strcpy(oraCN.arr,"system/[email protected]");
oraCN.len = strlen(oraCN.arr);
oraCN.arr[oraCN.len]=‘\0‘;
EXEC SQL CONNECT :oraCN;
printf("\n [OK Connected!] ");
return 0;
}
代碼其實是Windows的原版。
四、編譯運行
無需修改任何參數檔案,即安裝後直接建立main.pc,執行如下命令:
$ proc parse=none iname=main.pc
Pro*C/C++: Release 9.2.0.4.0 - Production on Thu Jun 7 14:17:05 2007
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
System default option values taken from: /home/ora/ora9/oracle/precomp/admin/pcscfg.cfg
$ gcc -g -o main main.c -I/home/ora/ora9/oracle/precomp/public -L/home/ora/ora9/oracle/lib -lclntsh
$ ./main
<ERROR> ORA-12541: TNS:no listener
成功編譯運行,這裡也可以使用《ProC動態SQL樣本(第1,2,3種方法)》一文中的樣本,
將//注釋全部替換為空白,即可編譯。
http://blog.csdn.net/liwei_cmg/archive/2006/05/29/759963.aspx
不過會有警示提示:
/tmp/ccC7E6qe.o(.text+0xea): In function `db_connect‘:
/home/ora/develop/src/db.c:385: the `gets‘ function is dangerous and should not be used.
這個是由於使用了gets函數所致,見gets的man手冊:
BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.
It is not advisable to mix calls to input functions from the stdio
library with low - level calls to read() for the file descriptor asso-
ciated with the input stream; the results will be undefined and very
probably not what you want.
要解決這個問題,可以使用scanf函數替換gets,擷取螢幕輸入。如 scanf("%s",cmd)。
原文地址:http://blog.chinaunix.net/uid-10376640-id-2960119.html
ProC第二彈