Oracle Python

Bulk insert to Oracle with Python

投稿日:

Overview

There is an example code to insert data in bulk to Oracle database with python cx_Oracle

Environments

python 3.7.3
Oracle 18c Express Edition

Step1 : Creating the table

Creating the table ‘oracle_insert’ in the schema ‘USER01’.

CREATE TABLE USER01.ORACLE_INSERT(
	col1 int
	,col2 int
	,col3 int
)

Step2 : Insert in bulk with executemany

  • dataset is the dataset to be inserted
  • Making a connection to database with cx_Oracle
  • Inserting in bulk with using cur.executemany()
import cx_Oracle

dataset =[
    [1,2,3]
    ,[4,5,6]
    ,[7,8,9]
    ,[10,11,12]
    ,[13,14,15]
    ,[16,17,18]
    ,[19,20,21]
] 

HOST = 'localhost'
PORT = '1521'
DB_NAME = 'xepd1'
USER = 'user01'
PASSWORD = 'password01'
SERVICE_NAME = 'xepdb1'
 
tns = cx_Oracle.makedsn(HOST, PORT, service_name =SERVICE_NAME)
conn = cx_Oracle.connect(USER,PASSWORD,tns)
cur = conn.cursor()
cur.executemany("insert into user01.oracle_insert (col1, col2, col3) values (:1, :2 ,:3)", dataset)
conn.commit()
conn.close()

Summary

The code to insert some rows in bulk is not difficult and simple. But the performance about it is not tested in here, I’ll try it in future.

-Oracle, Python
-,

執筆者:


comment

Your email address will not be published. Required fields are marked *

関連記事

【Python】From DataFrame To list type

Contents1 Overview2 Example table in databse3 Solution Overview Pandas can get data from a database with read_sql easily.Here we can show how to convert dataframe to list type for only one row from database. Example table in databse a,b,c are columns of this test_table. abc110100220200330300440400test_table Let’s get only “a” column from this table with pandas.These are sample code. import pandas as pd import psycopg2 connection = psycopg2.connect(host=’host’, dbname=’database’, user=’username’, password=’password’) df = pd.read_sql("SELECT a FROM test_table", connection) df.head() Result If you want to convert this result from dataframe to list type, you may think like [1, 2, 3, 4]. There …

Python : Insert dataframe data into MySQL table 

Contents1 Overview2 Environments3 Step1 : Making the table4 Step2 : Making data5 Step3 : Inserting data into MySQL table6 Additional explanation Overview Dataframe type in python is so useful to data processing and it’s possible to insert data as dataframe into MySQL . There is a sample of that. Environments Python 3.7.3MySQL 5.5.62 Step1 : Making the table Defining a table like the following. > CREATE DATABASE testdb; > CREATE TABLE testdb.mysql_table( col1 int ,col2 int ,col3 int ); Step2 : Making data Making data for insert from python. the data should be the same type as the table you …

Anaconda installation on CentOS

Contents1 Overview2 Environments3 What is Anaconda4 Step1 : pyenvのインストール5 Step2 : Installation of Anaconda3 Overview Anaconda package is very useful tool for data analysis by python language. This article shows how to install it on CentOS. Environments OS:CentOS7Anaconda3:3-5.3.1 What is Anaconda Nowadays the usage of python language is being more popular than before for the purpose of machine learning or processing data. Anaconda package is consists of not only python engine but also IDE, web-base editor or some useful tools. When you just install anaconda, you can use these bundled useful tools. CentOS7 has python 2 as a default but …

【Oracle】How to fix ORA-65096 at creating user

Contents1 Ovewivew2 Environments3 Error4 Step1 : Checking database 5 Step2 : Create user again6 Summary Ovewivew I encountered ORA-65096 when I tried to create user at Oracle 18c Express Edition. This is a example to fix ORA-65096. Environments OS : Windows10Oracle : 18c Express Edition Error SQL> CREATE USER user01 identified by password01 default tablespace USERS; ORA-65096: ORA-65096: invalid common user or role name Step1 : Checking database Oracle has two databases ,’CDB’ and ‘PDB’. It seems it’s not possible to create local user on CDB, so we have to check which database are used. SQL> show con_name; CON_NAME —————————— …

【Python】Changing images periodically with tkinter

Contents1 Overview2 Python library to be used3 Step1 : Display a image with tkintertkinter4 Step2 : Changing images5 Summary Overview There is a good library in python to make GUI, that is tkinter. It can display images too. Here, we are trying to change images periodically with tkinter. Python library to be used Following three libraries are used. tkinter ‘tkinter’ is used to make GUI. PIL(pillow) ‘PIL’ is used to deal with images in the python. threading ‘threading’ is used to change images automatically. Detail about that will be explained later. By the way, if you are using anaconda which …

Language Switcher

Categories