search
HomeBackend DevelopmentPython TutorialConfigure python to connect to oracle, read excel data and write it to the database

Configure python to connect to oracle, read excel data and write it to the database

Prerequisite: Oracle single instance has been installed locally, and you can use plsql developer to connect, or you can use TNS connection string to remotely connect to the oracle cluster

Read excel and write There are many ways to enter the database. The one introduced here is to use pandas to write, which is relatively simple. There is no need to organize the data after reading excel

(free learning recommendation: python video tutorial

The whole process needs to be carried out in two steps:

1. Configure python Connect to Oracle and test successfully

There are many tutorials on the Internet, but most of them are not so detailed, and they do not explain the difference between connecting to a single instance and connecting to a cluster. Here we first introduce the method of connecting to an Oracle single instance. , and the method of connecting to the Oracle cluster will be added later.

Version:
window 10 64-bit
python 3.6.8
cx-Oracle 7.3.0

Installation process:

1 . Use pip to install the package that operates Oracle:

pip install cx_Oracle==7.3.0

Configure python to connect to oracle, read excel data and write it to the database

##2. Manually configure cx_Oracle temporarily Client:

Note that the computer here is 64-bit, and the instant client used is also 64-bit. For 32-bit, you need to go to the download address below to find it

2.1. Unzip the following file

Link: https://pan.baidu.com/s/1ARcdK8kgNKfQsNbP5ZEbKg

Extraction code: lz0g

Configure python to connect to oracle, read excel data and write it to the database oracle instant client Client usage instructions:
https://docs.oracle.com/en/database/oracle/oracle-database/19/lnoci/instant-client.html#GUID-6895DB45-97AA-4738-9959-BD677D610186
Oracle instant client download address:
https://www.oracle.com/database/technologies/instant-client/downloads.html

2.2. Place it in a certain location on the D drive, for example:

Configure python to connect to oracle, read excel data and write it to the database

2.3. Configure environment variables

Control Panel-System and Security-System


Configure python to connect to oracle, read excel data and write it to the database
Configure python to connect to oracle, read excel data and write it to the database
Configure python to connect to oracle, read excel data and write it to the database
Configure python to connect to oracle, read excel data and write it to the database

2.4. Restart the computer to make the newly configured environment variables take effect

2.5. Test whether the configuration is successful

Although importing cx_Oracle has Red wavy lines are generally considered to be unsuccessful in importing, but you can ignore it here and run the test code directly. If there is no error, it means there is no problem.

If no data is found, it may be that there is no emp table under the user.

import cx_Oracleimport os# 设置环境编码方式,可解决读取数据库中文乱码问题os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'# 用户名/密码@IP:端口/实例名conn = cx_Oracle.connect('kplin/12sss3456@192.168.124.102:1521/ORCL')cursor = conn.cursor()try:
    sql = 'select * from emp'
    cursor.execute(sql)
    ret = cursor.fetchall()
    print(ret)
    # cursor.commit()except Exception as e:
    print(e)finally:
    cursor.close()

Configure python to connect to oracle, read excel data and write it to the database

2. Use pandas to read excel data and use sqlalchemy to assist in writing to the database

1. Install sqlalchemy , pandas

The pandas version is specified here because the latest version of pandas will report some strange errors when reading and writing excel. Just change to version 1.1.4.

pip install pandas==1.1.4pip install sqlalchemy

2. Prepare an excel table, named test.xlsx, and write the following test data

Configure python to connect to oracle, read excel data and write it to the database

3 , test reading and writing to the database

#!/usr/bin/env python# -*- coding:utf-8 -*-# date: 2021/3/14# filename: excel_to_db# author: kplinimport pandas as pdfrom sqlalchemy import create_enginefrom sqlalchemy import types# conn_string='oracle+cx_oracle://user:pass@host:port/dbname'conn_string='oracle+cx_oracle://KPLIN:654321@192.168.124.6:1521/ORCL'engine = create_engine(conn_string, echo=False)df = pd.read_excel('test.xlsx')# if_exists有三个可选值,'fail':如果存在该表则报错,'append':如果存在该表则将数据追加到列尾,'replace':如果存在该表则替换# df.to_sql('test', con=engine, if_exists='replace')# 按上面这种写入方式name字段将被写成clob字段类型,# 如果我们希望把name改为varchar2类型,怎么做?# 我们可以利用sqlalchemy的types把name指定为varchar2()类型len = df.name.str.len().max()df.to_sql('test', engine, if_exists='replace', dtype={'name': types.VARCHAR(len)})rows = engine.execute("SELECT * FROM TEST").fetchall()print(rows)

Related free learning recommendations: python tutorial( video)

The above is the detailed content of Configure python to connect to oracle, read excel data and write it to the database. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools