Python 操作 Oracle 数据库(cx_Oracle)

Oracle 数据库数据类型

这里写图片描述

----创建TF_B_AIR_CONFIG表
create table ECS_STORE.TF_B_AIR_CONFIG(
  TYPE_ID            VARCHAR2(20) not null,
  PROVINCE_CODE      VARCHAR2(4) not null,
  PARAMETER_TYPE     VARCHAR2(2) not null,
  LIMIT_NUM          VARCHAR2(4) not null,
  EFFECTIVE_FALG     VARCHAR2(2) default '1',
  UPDATE_TIME        DATE default sysdate,
  constraint TF_B_AIR_CONFIG_PK primary key(TYPE_ID , PROVINCE_CODE)--复合主键  
)
def table_exists(cursor,table_name):        #这个函数用来判断表是否存在
    stmt = "select Table_name from user_tables"
    cursor.execute(stmt)
    tables = cursor.fetchall()
    # table_list = re.findall('(\'.*?\')',str(tables))  #('OL$NODES',)
    # table_list = [re.sub("'",'',each) for each in table_list]  # "'LOGMNR_SESSION_EVOLVE$'"
    # tables = [table[0].strip("'") for table in tables]
    tables = {table[0].strip("'") for table in tables}
    if table_name.upper() in tables:
        return 1        #存在返回1
    else:
        return 0

Engine Configuration

Engine Configuration
这里写图片描述

engine = create_engine('oracle://scott:[email protected]:1521/sidname')
engine = create_engine('oracle+cx_oracle://scott:tiger@tnsname')

更多请见:
Oracle

oracle错误 ORA-01400: 无法将 NULL 插入 (“ROOT”.”TASK”.”ID”)的解决方案

-- Create sequence 
create sequence SEQ_NEW_PREDICT_201809
minvalue 1    -- 最小值=1
maxvalue 999999999999999999999999999  -- 指定最大值 
-- 或nomaxvalue      -- 没有最大值
-- NOCYCLE;      -- 不循环
start with 1   -- 从1开始
increment by 1  -- 每次递增1
cache 20;

--触发器
create or replace trigger TRI_NEW_PREDICT_201809
  before insert on NEW_PREDICT_201809  
REFERENCING OLD AS "OLD" NEW AS "NEW" FOR EACH ROW
begin
    SELECT SEQ_NEW_PREDICT_201809.NEXTVAL INTO :NEW.ID FROM DUAL;
end;

64-bit Oracle Client library cannot be loaded.md

Download an Oracle 12.2 “Basic” or “Basic Light” zip file: 64-bit matching your application architecture.
Unzip the package into a single directory that is accessible to your application. For example:

mkdir -p /opt/oracle
cd /opt/oracle
unzip instantclient-basic-linux.x64-12.2.0.1.0.zip

Install the libaio package with sudo or as the root user. For example:

sudo apt-get install libaio1

set the environment variable LD_LIBRARY_PATH to the appropriate directory for the Instant Client version. For example:

vi ~/.bashrc
    export LD_LIBRARY_PATH=/opt/oracle/instantclient_12_2:$LD_LIBRARY_PATH
source ~/.bashrc

python 连接 Oracle 插入中文乱码问题(cx_Oracle)

用python连接Oracle是总是乱码,最后发现时oracle客户端的字符编码设置不对。

编写的python脚本中需要加入如下几句:

import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

这样可以保证select出来的中文显示没有问题。

客户端的NLS_LANG设置及编码转换
①在Oracle客户端向服务器端提交SQL语句时,Oracle客户端根据NLS_LANG和数据库字符集,对从应用程序接传送过来的字符串编码进行转换处理。如果NLS_LANG与数据库字符集相同,不作转换,否则要转换成数据库字符集并传送到服务器。服务器在接收到字符串编码之后,对于普通的CHAR或VARCHAR2类型,直接存储;对于NCHAR或NVARCHAR2类型,服务器端将其转换为国家字符集再存储。

python 关于为datatime 对象添加时区信息以及时区转换问题

from pytz import utc  
from pytz import timezone  
from datetime import datetime  

cst_tz = timezone('Asia/Shanghai')  
utc_tz = timezone('UTC')  

now = datetime.now().replace(tzinfo=cst_tz)  
#local_dt = cst_tz.localize(now, is_dst=None)  
utctime = now.astimezone(utc)  

print "now   : %s"%now  
print "format: %s"%now.strftime('%Y-%m-%d %H:%M:%S')  
print "utc   : %s"%utctime  

utcnow = datetime.utcnow()  
utcnow = utcnow.replace(tzinfo=utc_tz)  
china = utcnow.astimezone(cst_tz)  

print "utcnow: %s"%utcnow  
print "format: %s"%utcnow.strftime('%Y-%m-%d %H:%M:%S')  
print "china : %s"%china  

更多错误提示链接

oracle错误(一) ORA-各种常见java.sql.SQLException小结

猜你喜欢

转载自blog.csdn.net/Dooonald/article/details/82468403