1. 程式人生 > >python3 upload data from CSV into Oracle

python3 upload data from CSV into Oracle

+= log values encoding utf-8 ade count lin win

1. 使Python可以操作Oracle數據庫,首先需要安裝cx_Oracle包。
2. 創建一個簡單的python文件,測試安裝是否成功。

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 
 4 import cx_Oracle  
 5 dns_tns=cx_Oracle.makedsn(host,port,service_name=servic name)  
 6 con = cx_Oracle.connect(username, password, dns_tns)  
 7 cur=con.cursor()  
8 sql = "select * from test_table" 9 cur.execute(sql) 10 re = cur.fetchall() 11 for item in re: 12 print(item) 13 cur.close() 14 con.close()

3. 插入操作(少量的插入)

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 
 4 import cx_Oracle  
 5 import csv
 6 
 7 dns_tns=cx_Oracle.makedsn(host,port,service_name=
service name) 8 con = cx_Oracle.connect(username, password, dns_tns) 9 cur=con.cursor() 10 with open(/home/cyn/test.csv, newline=‘‘, encoding=utf-8) as f: 11 data_reader = csv.reader(f) 12 for row in data_reader: 13 sql = "INSERT INTO runbook_pid_w_user(host_name, account, host_type) VALUES (‘
" + row[0] +"‘, ‘" + row[1] +"‘, ‘Windows Server‘)" 14 print(sql) 15 cur.execute(sql) 16 con.commit() 17 cur.close() 18 con.close()

4. 當插入的data數量很多時,可以用executemany來進行插入操作。

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 
 4 import cx_Oracle  
 5 import csv
 6 
 7 dns_tns=cx_Oracle.makedsn(host,port,service_name=service name)  
 8 con = cx_Oracle.connect(username, password, dns_tns)  
 9 cur=con.cursor()
10 with open(/home/cyn/test.csv, newline=‘‘, encoding=utf-8, errors="ignore") as f:
11     data_reader = csv.reader(f)
12     M = []
13     i = 0 
14     sql = "INSERT INTO runbook_pid_pim(host_name, account, host_type) VALUES (:1, :2, :3)"
15     cur.prepare(sql)  
16     for row in data_reader:
17         i += 1
18         if i!=1:
19                 M.append((row[0],row[1],row[2]))
20         if i % 500 == 0:           
21             print(i)
22             cur.executemany(None, M)
23             M = []
24     cur.executemany(None, M)
25     print(i)
26 con.commit()
27 cur.close()
28 con.close()

python3 upload data from CSV into Oracle