Last active
May 21, 2020 14:27
-
-
Save Harish2997/2f0cce86bacfb11d2706789e66910f1c to your computer and use it in GitHub Desktop.
The python script establishes a connection with oracle, gets the CSV data and inserts into the table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cx_Oracle | |
import pandas as pd | |
import csv | |
import datetime | |
import os | |
import time | |
connectStart = time.time() | |
r = connection = cx_Oracle.connect('username','password','localhost:1521/XE') | |
print(r) | |
r = cursor = connection.cursor() | |
print(r) | |
connectEnd = time.time() | |
print("Time taken to connect to DB >>> "+str(connectEnd-connectStart)) | |
schema= input('Enter schema name :') | |
pandasRStart = time.time() | |
csvfile = r'D:\Python\1MRow.csv' | |
data = pd.read_csv(csvfile,sep=',', error_bad_lines=False, index_col=False, dtype='unicode') | |
print(data) | |
pandasREnd = time.time() | |
print("Time taken to read csv to Dataframe >>> "+str(pandasREnd-pandasRStart)) | |
tablename = input("Enter table name : ") | |
tablename = tablename.replace(' ','_') | |
TableVStart = time.time() | |
try: | |
cursor.execute('truncate table ' + schema + '.' + tablename) | |
except: | |
print("Table Does Not Exist") | |
print("Creating Table") | |
tableQuery = "" | |
for col in range(len(data.columns)): | |
column = data.columns[col] | |
maxLength = int(data[column].str.encode(encoding='utf-8').str.len().max()) | |
if (col != (len(data.columns)-1)): | |
tableQuery = tableQuery+""+(data.columns[col]+" VARCHAR2("+str(maxLength)+"),") | |
else: | |
tableQuery = tableQuery+""+(data.columns[col]+" VARCHAR2("+str(maxLength)+")") | |
cursor.execute('create table ' + schema + '.' + tablename +" ( "+tableQuery+" )") | |
print("Table created Successfully") | |
tableVEnd = time.time() | |
print("Time taken to validate Table in DB >>> "+str(tableVEnd-TableVStart)) | |
reader = csv.reader(open(csvfile), delimiter=',') | |
L = [] | |
column_list='' | |
value_list='' | |
first_row = next(reader) | |
column_string = ','.join(first_row) | |
insertQStart = time.time() | |
insert_string='insert into ' + schema + '.' + tablename + ' (' + column_string + ') values (' | |
val_list=[] | |
for i in range(1,len(first_row)+1): | |
val_list.append(':'+ str(i)) | |
value_string=','.join(val_list) | |
insert_string += value_string + ')' | |
for row in reader: | |
for index,col in enumerate(row): | |
col_tr = col | |
row[index] = col_tr | |
L.append(row) | |
cursor.prepare(insert_string) | |
cursor.executemany(None, L) | |
print('Inserted: ' + str(cursor.rowcount) + ' rows.') | |
insertQEnd = time.time() | |
print("Time taken to insert data to Table"+str(insertQEnd - insertQStart)) | |
connection.commit() | |
cursor.close() | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment