python3 upload data from CSV into Oracle

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3 upload data from CSV into Oracle相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

rsync 精确同步文件用法 (转载)

GridFS.put() 和 GridFSBucket.upload_from_stream() 之间的区别

ci多表查询,跳转,访问方式

Flask开发 导入flask_uploads包时提示“IMPORTERROR: CANNOT IMPORT NAME ‘SECURE_FILENAME‘ FROM ‘WERKZEUG‘“

Flask开发 导入flask_uploads包时提示“IMPORTERROR: CANNOT IMPORT NAME ‘SECURE_FILENAME‘ FROM ‘WERKZEUG‘“

UserListError.ADVERTISER_NOT_WHITELISTED_FOR_USING_UPLOADED_DATA @ 操作