python3:使用cx_Oracle 操作Oracle数据库

1,安装 cx_Oracle 注意版本号 要和python版本一致
pip install cx_Oracle

2安装 Oracle Instant Client
会报 DPI-1047: 64-bit Oracle Client library cannot be load
下载地址 (注意版本)
https://www.oracle.com/database/technologies/instant-client/downloads.html
解压之后将安装文件里的.dll文件 全部复制到python安装目录 和python.exe 一级
如果没有做这一步 会报 cx_Oracle.DatabaseError: DPI-1072: the Oracle Client library version is unsupported
有的文章说 设置环境变量 我试了 没用 然后删了 最后直接将.dll全部拷贝到python目录,然后成功了 具体为什么 我也不知道…

3,测试程序

import cx_Oracle

import os
import csv

from log.mylog import getMyLogger

log = getMyLogger(__name__)
# 中文乱码处理
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.ZHS16GBK'
if __name__ == '__main__':
    # 数据库连接对象
    conn = cx_Oracle.connect('用户名/密码@ip地址:端口号/ora11g')
    # 数据库执行对象
    curs = conn.cursor()
    # sql语句
    sql = 'select * from XXXXX '
    # sql执行结果
    result = curs.execute(sql)
    # 获取表的列名
    colsname = curs.description
    title = [i[0] for i in colsname]
    print(title)
    i = 1
    for item in result:
        log.info('%s   %s', i, item)
        i = i + 1

    # 将数据写入CSV文件   网上抄的  我没成功
    try:
        with open("table_name.csv", 'w') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(title)
            writer.writerows(result)
    except Exception as a:
        print("文件写入数据错误", a)
    curs.close()
    conn.close()

勉强可以有点实际应用的的:

import cx_Oracle

import os

import openpyxl
from openpyxl import Workbook
from log.mylog import getMyLogger

log = getMyLogger(__name__)
# 中文乱码处理
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.ZHS16GBK'


class OracleOper(object):
    def __init__(self, url='用户名/密码@ip地址:端口号/ora11g'):
        self.__conn = cx_Oracle.connect(url)
        self.curs = self.__conn.cursor()

    def execute_sql(self, exe_sql='select 1 from dual'):
        execute_result = self.curs.execute(exe_sql)
        title = [t[0] for t in self.curs.description]
        result_list = [tuple(title)]
        i = 0
        for item in execute_result:
            result_list.append(item)
            i = i + 1
        self.__close()
        print('查询总数', len(result_list) - 1)
        return result_list

    def __close(self):
        self.curs.close()
        self.__conn.close()


def add_excel(data_tuple, file_name='新建表格', sheet_name='sheet1'):
    path = os.getcwd()  # 获取当前路径
    file_path = path + '\\' + file_name + '.xlsx'
    if not os.path.exists(file_path):
        wb = Workbook()
        wb.save(file_path)
    wb = openpyxl.load_workbook(file_path)
    wb.create_sheet(title=sheet_name, index=0)
    table = wb.worksheets[0]
    nrows = 1
    for tupite in data_tuple:
        ncolumns = 0
        for item in tupite:
            ncolumns = ncolumns + 1
            table.cell(nrows, ncolumns, item)  # 行,列,值 这里是从1开始计数的
        nrows = nrows + 1
    wb.save(file_path)  # 一定要保存
    print(file_name + '已保存')


if __name__ == '__main__':
    # 预发
    # pre_url = '用户名/密码@ip地址:端口号/ora11g'
    oracle_oper = OracleOper()
    sql_str = '''
	select aaa  from dual
    '''
    result_list = oracle_oper.execute_sql(sql_str)
    add_excel(result_list, '文件名', 'sheet名')

大数据量有时会出现一个bug

error
Traceback (most recent call last):
  File "D:/Work/Data/OracleData/OracleServer/oracleServer.py", line 99, in <module>
    add_excel(result_list, '1111', sheet_name)
  File "D:/Work/Data/OracleData/OracleServer/oracleServer.py", line 53, in add_excel
    table.cell(nrows, ncolumns, item)  # 行,列,值 这里是从1开始计数的
  File "C:\Software\Python\lib\site-packages\openpyxl\worksheet\worksheet.py", line 240, in cell
    cell.value = value
  File "C:\Software\Python\lib\site-packages\openpyxl\cell\cell.py", line 219, in value
    self._bind_value(value)
  File "C:\Software\Python\lib\site-packages\openpyxl\cell\cell.py", line 195, in _bind_value
    value = self.check_string(value)
  File "C:\Software\Python\lib\site-packages\openpyxl\cell\cell.py", line 162, in check_string
    raise IllegalCharacterError
openpyxl.utils.exceptions.IllegalCharacterError

使用pandas 写入更快
#将数据库查询到的数据塞入
execute_result = curs.execute(exe_sql)
title = [t[0] for t in self.curs.description]
df = pd.DataFrame(execute_result, columns=title)
写入到excel
df1.to_excel(‘excel_name.xlsx’, sheet_name=‘sheet1’, engine=‘xlsxwriter’)
# 写入csv
df.to_csv(‘csv_name.csv’)
# 写入html
df.to_html(‘csv_name.html’)

你可能感兴趣的:(Python)