码迷,mamicode.com
首页 > 数据库 > 详细

python3 upload data from CSV into Oracle

时间:2017-10-13 14:18:39      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:value   acl   oracle   import   简单   数据库   enc   erro   exec   

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

标签:value   acl   oracle   import   简单   数据库   enc   erro   exec   

原文地址:http://www.cnblogs.com/cyn413/p/7660534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!