极简ProC连接TimesTen程序

来源:互联网 发布:mysql workbench中文 编辑:程序博客网 时间:2024/04/29 16:56

TimesTen也支持Pro*C接口,从编程的角度来看,除了连接字符串的指定外,其它和针对Oracle的开发几乎一样。

Pro*C程序连接TimesTen的框架

我们先来看一下Pro*C程序连接TimesTen的框架:

int main(int argc, char** argv){// 连接数据库,可以用以下三种方法之一// 法一    EXEC SQL CONNECT :user IDENTIFIED BY :pass USING :svc;// 法二    EXEC SQL CONNECT :connstr;// 法三       EXEC SQL CONNECT :user IDENTIFIED BY :pass USING :easyconnstr;// 执行DDL    EXEC SQL CREATE TABLE a (a int);    // 执行DML        EXEC SQL INSERT INTO a values(12345);   // 断开连接    EXEC SQL COMMIT WORK RELEASE;    exit(0);}

Pro*C程序连接TimesTen的三种方法

  1. EXEC SQL CONNECT :user IDENTIFIED BY :pass USING :svc;
    法1是最传统的方法,svc指定的service在tnsnames.ora中定义

  2. EXEC SQL CONNECT :connstr;
    法2是法1的一个变种,connstr的形式为”username/password@service”, service也是在tnsnames.ora中定义,例如
    “tthr/timesten@tnssampledb_1122”;

  3. EXEC SQL CONNECT :user IDENTIFIED BY :pass USING :easyconnstr;
    法3有些特别,不用TNS协议,而是直接使用easy connect字符串。
    easy connect的通用格式为[//]host[:port]/service_name:server[/instance]。
    TimesTen用的没这么复杂,简化的格式为hostname/DSN:driver, driver为timesten_client 或 timesten_direct,例如:
    “localhost/sampledb_1122:timesten_direct”;

我还是建议使用TNS,TNS相当于为应用加了一层虚拟化接口,变动接口时,可以不用改源程序,比较灵活。

预编译Pro*C程序

首先,预编译,是将.pc程序转化为.c程序

$ proc iname=helloworld.pc Pro*C/C++: Release 11.2.0.2.0 - Production on Thu Jul 14 07:31:58 2016Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.System default option values taken from: /u01/app/oracle/product/11.2.0/dbhome_1/precomp/admin/pcscfg.cfg$ ls -l helloworld.c-rw-rw-r-- 1 oracle oracle 6678 Jul 14 07:31 helloworld.c

编译和链接

export INSTANT_CLIENT=$TT_HOME/ttoracle_home/instantclient_11_2$ gcc -c helloworld.c -I $INSTANT_CLIENT/sdk/include$ ll helloworld.o-rw-rw-r-- 1 oracle oracle 5904 Jul 14 08:17 helloworld.o$ gcc -o helloworld helloworld.o -L $INSTANT_CLIENT -lclntsh$ ll helloworld-rwxrwxr-x 1 oracle oracle 10446 Jul 14 08:18 helloworld

在此阶段定义了INSTANT_CLIENT,所谓instant client,就是一个简版的oracle client。

Oracle Instant Client is a light-weight software which helps to run OCI, OBDBC, Pro*C, JDBC without installing full Oracle client.

这就很好理解TimesTen自带的sqlplus可以连接Oracle数据库了。

执行程序

这个程序的逻辑和之前的 极简Java连接TimesTen程序 是一样的。

$ ./helloworldCommand> select * from a;< 12345 >[oracle@timesten-hol proc]$ ./helloworld ORACLE error--ORA-00955: TT2207: Table A already exists -- file "plittddl.c", lineno 737, procedure "plittCreate"Command> drop table a;Command> select * from a;< 12345 >$ ./helloworldCommand> select * from a;< 12345 >

总结

Pro*C可以很简单的连接TimesTen, 和连接Oracle没有区别,不过我还是更建议不用 Pro*C,不伦不类的。
要性能,你可以用C,要简单和灵活性,你就用Java好了。

附录 helloworld.pc程序

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <sqlda.h>#include <sqlcpr.h>void sql_error(char *msg);int main(int argc, char** argv){    char  user[32] = "tthr";    char  pass[32] = "timesten";    char  svc[32] = "tnssampledb_1122";    char connstr[128] = "tthr/timesten@tnssampledb_1122";    char easyconnstr[128] = "localhost/sampledb_1122:timesten_direct";    EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");//  EXEC SQL CONNECT :user IDENTIFIED BY :pass USING :svc;    EXEC SQL CONNECT :connstr;//  EXEC SQL CONNECT :user IDENTIFIED BY :pass USING :easyconnstr;    EXEC SQL CREATE TABLE a (a int);        EXEC SQL INSERT INTO a values(12345);       EXEC SQL COMMIT WORK RELEASE;    exit(0);}void sql_error(char *msg){    char err_msg[128];    size_t buf_len, msg_len;    EXEC SQL WHENEVER SQLERROR CONTINUE;    printf("\n%s\n", msg);    buf_len = sizeof (err_msg);    sqlglm((unsigned char *) err_msg, &buf_len, &msg_len);    printf("%.*s\n", (int) msg_len, err_msg);    EXEC SQL ROLLBACK RELEASE;    exit(EXIT_FAILURE);}

附件 tnsnames.ora

tnssampledb_1122 =(DESCRIPTION=(CONNECT_DATA = (SERVICE_NAME = sampledb_1122)(SERVER = timesten_direct)))

附件 DSN定义

这个是缺省安装自动产生的DSN,没有做修改

[sampledb_1122]Driver=/home/oracle/TimesTen/tt1122/lib/libtten.soDataStore=/home/oracle/TimesTen/tt1122/info/DemoDataStore/sampledb_1122PermSize=40TempSize=32PLSQL=1DatabaseCharacterSet=US7ASCII

Makefile

SHELL=/bin/shTT_MVL_LINUX    = 0INSTDIR         = /home/oracle/TimesTen/tt1122COMMDIR         = $(INSTDIR)/quickstart/sample_code/commonTTORACLE_HOME   = $(INSTDIR)/ttoracle_home/instantclient_11_2OCIINCS         = $(TTORACLE_HOME)/sdk/includeCC              = gccPLATCFLAGS      = -Os -finline-functionsLDFLAGS         =EXTRALIBS       = -lpthread -lm -lrtifeq ($(TT_MVL_LINUX),1)EXTRALIBS       += -ldl -lnsl -lnnz11endifINCS        = -I$(INSTDIR)/include -I$(COMMDIR) -I$(OCIINCS)CSDEFS          = -DTTCLIENTSERVERCFLAGS          = $(PLATDEFS) $(PLATCFLAGS) $(INCS)TTLINK           = -L$(INSTDIR)/lib -L$(TTORACLE_HOME) -Wl,-rpath,$(INSTDIR)/libICLIBS          = $(TTLINK) -lclntsh  $(EXTRALIBS)HELLOWORLD  = helloworldHELLOWORLDOBJS = helloworld.oPROGS           = $(HELLOWORLD)C_FILES         = helloworld.c## Top level targets#all: $(PROGS)clean:    rm -f $(PROGS) *.o *.lis $(C_FILES)## Targets for building executables#$(HELLOWORLD): $(HELLOWORLDOBJS)    $(CC) -o $@ $(LDFLAGS) $(HELLOWORLDOBJS) $(ICLIBS)## Target for building object files#.c.o:    $(CC) $(CFLAGS) -o $@ -c $<## Targets for Oracle Pro*C Pre-compilation#helloworld.c: helloworld.pc    @proc include=$(COMMDIR) iname=helloworld.pc config=pcscfg.cfg 

补充:第四中连接方法TWO_TASK

在连接串中可以不指定服务,如下面的形式:

EXEC SQL CONNECT :user IDENTIFIED BY :pass

然后在环境变量中指定数据库名,环境变量的值既可以是TNS名,也可以是easy connect字符串。

环境变量的名字在Unix/Linux下是TWO_TASK,在Windows下是LOCAL。

如下例. 先测试TNS名:

[oracle@timesten-hol proc]$ unset TWO_TASK[oracle@timesten-hol proc]$ ./helloworld ORACLE error--ORA-00955: name is already used by an existing object$ export TWO_TASK=tnssampledb_1122$ ./helloworld Command> select * from a;< 12345 >Command> drop table a;

再测试easy connect 字符串:

$ export TWO_TASK='localhost/sampledb_1122:timesten_direct'$ echo $TWO_TASKlocalhost/sampledb_1122:timesten_direct$ ./helloworld Command> select * from a;< 12345 >

参考

  • TimesTen C Developer’s Guide
  • http://psoug.org/oraerror/PCC-02362.htm
  • [Oracle Instant Client](http://www.oracle.com/techne
  • twork/database/features/instant-client/index-097480.html)
  • https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:89412348059
  • Introduction to Pro*C
0 0
原创粉丝点击