py獲取oracle中table數據

利用python獲取oracle中的一個table的數據,並存爲dataframe,格式與table完全一樣,之後就可以進行分析啦!後續有時間再來補充後續代碼

# -*- coding: utf-8 -*-
"""
Created on Sun Feb 24 22:05:49 2019

@author: Robin Yao

該代碼利用之前查找出的4個出行特徵指標進行分析
"""
import cx_Oracle as oracle
import pandas as pd
import matplotlib as plt


def get_data():
    #該函數用來得到oracle中的表car_identified_index中的數據
    db = oracle.connect('C##***/**********@localhost:1521/orcl')    # connect oracle database
    print("database connected")
    cursor = db.cursor()    # create cursor
    cursor.execute('select * from car_identified_index')    # execute sql 
    data = cursor.fetchall()    # fetch data
    df = pd.DataFrame(data)

    #change columns' name
    df.rename(columns={"根據數據庫中的數據來對字段進行命名,格式: 0:'' ,1 :'' "}, inplace = True)
    #print(df)
    cursor.close()   #close the curse
    db.close()   #close the database
    print("database close")
    return df

def main():
    df = get_data()  #get the table car_identified_index
    #print(df)


if __name__ == '__main__':
    main()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章