python使用cx_Oracle连接timesten

环境:

OS:Windows/Linux

DB:18.1

 

 #################################################################windows################################################

1.配置环境变量
TNS_ADMIN=C:\TimesTen\tt181_64\network\admin

 

2.创建tnsnames.ora文件
在timesten安装目录下
C:\TimesTen\tt181_64\network\admin\samples
拷贝一个tnsnames.ora到C:\TimesTen\tt181_64\network\admin进行修改

 

3.修改tnsnames.ora
C:\TimesTen\tt181_64\network\admin\tnsnames.ora

添加如下内容:

mytest = (DESCRIPTION =
              (CONNECT_DATA =
                (SERVICE_NAME = timesten_dns)
                (SERVER = timesten_client)))

 

这里的timesten_dns是在odbc里创建的odbc数据源

 

4.测试代码

#!/usr/bin/env python
#coding=utf-8
import os, json, urllib, datetime, shutil,random,uuid
from random import choice
import pymysql
import traceback
import time

import cx_Oracle
import pyodbc

def connect_timesten_db():
    connection = cx_Oracle.connect("hxl", "oracle", "mytest")
    cursor = connection.cursor()
    cursor.execute("insert into tb_test values (42,42)")
    connection.commit()
    connection.close()


if __name__ == '__main__':
    print("开始时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    ##l_flag = insert_data_for_doris()
    l_flag=connect_timesten_db()
    print("结束时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

 

 

 ###################################################################linux################################################

说明:程序和timesten服务都在同一台机器,如不在同一台机器上的话,程序的机器也需要安装timesten,不启动服务,将服务器的配置(conf目录下的文件)拷贝过来

1.设置环境变量

su - instanceadmin

[instanceadmin@localhost ~]$ more .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
export TIMESTEN_HOME=/opt/tt18.1.4.34.0/myinstance/instance1
export ORACLE_HOME=/usr/lib/oracle/12.2/client64
export PATH=$ORACLE_HOME/bin:$TIMESTEN_HOME/myinstance/instance1/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/opt/tt18.1.4.34.0/lib:$LD_LIBRARY_PATH
TNS_ADMIN=/opt/tt18.1.4.34.0/network/admin

export PATH

 

测试程序

#!/usr/bin/env python
#coding=utf-8
import os, json, urllib, datetime, shutil,random,uuid
from random import choice
import pymysql
import traceback
import time

import cx_Oracle
import pyodbc

def connect_timesten_db():
    mytest = '(DESCRIPTION =(CONNECT_DATA =(SERVICE_NAME = sampledbCS)(SERVER = timesten_client)))'
    connection = cx_Oracle.connect("hxl", "oracle", mytest,encoding="UTF-8")
    cursor = connection.cursor()
    cursor.execute("insert into tb_test values (42,42)")
    connection.commit()
    connection.close()


if __name__ == '__main__':
    print("开始时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    ##l_flag = insert_data_for_doris()
    l_flag=connect_timesten_db()
    print("结束时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

 

posted @ 2023-06-02 15:27  slnngk  阅读(45)  评论(0编辑  收藏  举报