python将oracle数据库保存到excel

上代码:

import pandas  as pd
import SqlHelper.ORACLE as ORA

if __name__ == '__main__': 
    #连接数据库
    ms = ORA.ORACLE(host="localhost:1521",db="orcl",user="example",pwd="example")

    ########################################################## 方式一:先读取为二维list,再转dataframe,最后保存为Excel
    reslist = ms.ExecQuery("select * from version ")
    # list转dataframe
    df1 = pd.DataFrame(reslist, columns=['ID', 'VERSION', 'MESSAGE'])
    df1.to_excel("E:\\MyPrj\\Python\\Files\\Excel1.xlsx", index=False)

    ########################################################## 方式二:直接读取为dataframe,然后保存为Excel
    df2 = ms.ExecQueryToDataFrame("select * from version ")
    df2.to_excel("E:\\MyPrj\\Python\\Files\\Excel2.xlsx", index=False)

如果对您有帮助,请赞助根棒棒糖~

猜你喜欢

转载自www.cnblogs.com/shurun/p/11957937.html