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)

 

若是對您有幫助,請贊助根棒棒糖~spa

相關文章
相關標籤/搜索