php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 521|回复: 0

Python从excel导入数据到oracle

[复制链接]

2615

主题

2622

帖子

9281

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6544
贡献
0
注册时间
2021-4-14
最后登录
2024-4-19
在线时间
665 小时
QQ
发表于 2022-6-21 22:33:14 | 显示全部楼层 |阅读模式
[mw_shl_code=python,true]#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' Read excel and import to Oracle database '

import os  
import cx_Oracle  
import openpyxl

conn = cx_Oracle.connect("sa", "sa123", "ORA_Test")
cursor = conn.cursor()  

'''cursor.execute("select 'hello', 'world!' from dual")  
rows = cursor.fetchall()
for row in rows:
    print(row[0], row[1])
cursor.close()
conn.close()'''

excelPath = r"D:\TEST.xlsx"

class Listexcel(object):
    pass
def run():
        wb = openpyxl.load_workbook(excelPath)
        sheet = wb["Sheet1"]
        MachineName=""
        DrumValue=""
        lst =[]
        print(wb.encoding)
        sql = """insert into PyExcel2Ora values (:RowNo,:MachineName,rumValue,sysdate)"""
        for i in range(5,sheet.max_row+1):
                for j in range(1,3):
                        try:
                                if j==1:
                                        MachineName=sheet.cell(row=i,column=j).value
                                        #print(MachineName)
                                else:
                                        DrumValue=sheet.cell(row=i,column=j).value
                                        #print(DrumValue)
                        except Exception as ex:
                                print("rocess row {0} failure!,reason:{1}".format(i,str(ex)))
                if(not MachineName is None and not DrumValue is None):
                        #print(i)
                        listexcel=Listexcel()
                        listexcel.RowNo=i-4
                        listexcel.MachineName=MachineName
                        listexcel.DrumValue=DrumValue       
                        lst.append(listexcel)
                MachineName=""
                DrumValue=""               
                #print(sheet.cell(row=i,column=j).value)
        cursor.execute("Truncate table PyExcel2Ora")                
        for item in lst:
                cursor.execute(sql,{
                        'RowNo': item.RowNo,
                        'MachineName': item.MachineName,
                        'DrumValue': item.DrumValue.encode('utf-8')
                        })
                rowCount=cursor.rowcount
                if (rowCount <=0) :
                        print("Import row {0} failure!Machinename:{1}".format(item.RowNo+4,item.MachineName))
       
        conn.commit()               
        cursor.close()
        conn.close()                                               
run()
[/mw_shl_code]





上一篇:摩尔库伦强度准则
下一篇:matlab POD处理
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-4-20 07:01 , Processed in 0.176664 second(s), 44 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表