Python script that outputs all records of Oracle table to CSV file

A Python script I made earlier. Output the record of the specified table to a CSV file.

environment

See below for how to install cx_Oracle. http://mokky14.hatenablog.com/entry/2014/12/17/150854

script

tbl2csv.py


#!/usr/bin/python3

import cx_Oracle
import sys
import csv
from itertools import chain

argvs = sys.argv
argc = len(argvs)
if argc != 2:
  print('Usage: %s TableName' % argvs[0])
  quit()

table_name = argvs[1].upper()
file_name = table_name + '_data.csv'

with cx_Oracle.connect('scott','tiger','xx.xx.xx.xx/tns_service_name') as conn:
  #Get table column name
  column_name_sql = 'select column_name from user_tab_columns where table_name = :tbl'
  cur_columns = conn.cursor()
  cur_columns.execute(column_name_sql, tbl=table_name)
  columns = cur_columns.fetchall()
  cur_columns.close()
  columns = tuple(chain.from_iterable(columns))

  #Get all records in the table
  data_sql = 'select * from %s' % table_name
  cur_data = conn.cursor()
  cur_data.execute(data_sql)
  with open(file_name, 'w') as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(columns)
    while 1:
      rows = cur_data.fetchmany(50)
      if len(rows) == 0:
        break
      csv_writer.writerows(rows)
  cur_data.close()

Fetch 50 records in the table and output to a file. Processing ends when the number of fetches reaches 0. I feel that the way to write a fetch loop is not good enough. ..

Recommended Posts

Python script that outputs all records of Oracle table to CSV file
A python script that converts Oracle Database data to csv
Python script to create a JSON file from a CSV file
Output the specified table of Oracle database in Python to Excel for each file
A Python script that saves a clipboard (GTK) image to a file.
[Python] Write to csv file with Python
Output to csv file with Python
[Good By Excel] python script to generate sql to convert csv to table
[Python] How to convert db file to csv
[Python] Convert csv file delimiters to tab delimiters
[Python] How to read a csv file (read_csv method of pandas module)
Various ways to read the last line of a csv file in Python
Script python file
How to read a CSV file with Python 2/3
[For beginners] Script within 10 lines (7. Script that outputs csv from sqlite3 table with bash
A Python script that reads a SQL file, executes BigQuery and saves the csv
Writing logs to CSV file (Python, C language)
[Python] Scan the inside of the folder including subfolders → Export the file list to CSV
From Excel file to exe and release of tool that spits out CSV
One-liner that outputs 10000 digits of pi with Python
Speed evaluation of CSV file output in Python
Convert financial information of all listed companies for the past 5 years to CSV file
I tried to touch the CSV file with Python
A memorandum to run a python script in a bat file
One liner that outputs 1000000 digits of pi in Python
How to convert JSON file to CSV file with Python Pandas
A script that outputs a list of SoftLayer portal users
[Python] Outputs all combinations of elements in the list
Read Python csv file
A Python script that allows you to check the status of the server from your browser
Assigned scaffolding macro in Python script file to F12 key
[Python] How to output a pandas table to an excel file
A set of script files that do wordcloud in Python3
[Python3] List of sites that I referred to when I started Python
A Python script that compares the contents of two directories
Command to list all files in order of file name
Summary of python file operations
Write to csv with Python
Download csv file with python
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
The story that the version of python 3.7.7 was not adapted to Heroku
Python --Most elegant way to read lines of file into list
Shell script (Linux, macOS) that outputs the date of the last week
A story that struggled to handle the Python package of PocketSphinx
Conditional branch due to the existence of a shell script file
Create a shell script to run the python file multiple times
[Python] When you want to use all variables in another file
Normalize the file that converted Excel to csv as it is.
[Python] How to store a csv file as one-dimensional array data
A script that returns 0, 1 attached to the first Python prime number
I made a library that adds docstring to a Python stub file.
[python] A note that started to understand the behavior of matplotlib.pyplot
[Python] A program that rotates the contents of the list to the left