3.python 连接封装的数据库

from oracletest1 import MyOracle
import xlwt
import datetime
name='xxx'
password='xxx'
tnsname='xxx'
#连接数据库
myo = MyOracle(name,password,tnsname)
#建立指针
cur = myo.mycursor()
#查询数据
sql="select * from xxx where flag='0'"
#返回查询的表头和数据
tabtopname,row = myo.myquery(sql,cur)
#创建EXCLE
f = xlwt.Workbook()
sheet1 = f.add_sheet("op")
k=0
j=0
kk=0
print("写入头部信息")
#写入头部信息
for topname in tabtopname:
    sheet1.write(0,j,topname[0])
    j+=1
#写入数据内容
print("写入数据内容")
for g in row:
    kk+=1
    for h in g:
        if(h == "None"):
            h = ""       
        elif(isinstance(h,datetime.datetime)):
            h = "to_date('" + str(h) + "','YYYY-MM-DD HH24:MI:SS')"
        sheet1.write(kk,k,h)
        k+=1
    k=0
#保存数据
print("保存数据")
f.save("op.xls")
#关闭游标连接
myo.mycurclose(cur)
myo.myconnclose()

#时间格式转换
"""
print(row[0])
b=row[0]
print(b[17])
c=b[17]
if(isinstance(c,datetime.datetime)):
    temp = "to_date('" + str(c) + "','YYYY-MM-DD HH24:MI:SS')"
    print(temp)    
"""

  

posted @ 2022-02-25 16:46  种太阳  阅读(147)  评论(0编辑  收藏  举报