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()
发布了41 篇原创文章 · 获赞 40 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_39805362/article/details/87913695