实际工作中,对于数据导入导出可以使用PLSQL Developer/Tools/Import Tbales功能实现,但由于回滚段的限制对于导入的数据量存在限制。
可以通过修改文件中的语句,控制部分提交,但数据量大文件也大,打开文件设置提交也是件麻烦事。
所以编写一个Python脚本,读取文件中语句并进行部分提交,以Oracle数据库为基础。
import cx_Oracle db = cx_Oracle.connect(‘tssh/rdd3sjtest@sjtest‘) dblj = db.cursor() count = 0 ycts = 0 cgts = 0 str1 =‘‘ while(1 == 1): file_name = input("请输入文件名\n") print(2) fn = file_name[-3:].upper() if(fn != ‘SQL‘): print("文件名无效,必须是sql文件\n") continue fileobject = open(file_name) for line in fileobject: st = line[0:6].upper() print(line[0:6].upper()) if(st ==‘INSERT‘ or st == ‘VALUES‘): count = count + 1 str1 = str1 + line if(count == 2): str1 = str1.replace(‘;‘,‘‘) print(str1) try: dblj.execute(str1) dblj.commit() cgts = cgts + 1 except IOError: print("Error: 没有找到文件或读取文件失败") else: ycts = ycts + 1 count = 0 str1 = ‘‘ print("成功"+cgts+"条","失败"+ycts+"条") fileobject.close() dblj.close()
Python-工具-批量插入INSERT语句(将sql文件导入数据库)
原文:https://www.cnblogs.com/yangjn/p/11688355.html