python连接oracle数据库

python连接oracle通过cx-oracle包

安装cx_oracle:pip install cx_oracle

在执行这步时遇到个坑,找不到vc编译器

其实很简单,访问这个网址 http://www.microsoft.com/en-us/download/details.aspx?id=44266,下载安装一下就ok了

安装完毕后 import cx_oracle出错了,这个坑是自己抛的

o要大写!!要大写!!

import cx_Oracle

使用sqlalchemy

from sqlalchemy import *  
from sqlalchemy.sql import select  
from sqlalchemy.schema import *  
import numpy as np
import pandas as pd
import os

# create the engine for oracle db  
# we need to install cx_Oracle and sqlalchemy in advance.  
db_engine=create_engine('oracle://vehicle:VEHICLE@10.10.11.171:1521/orcl', echo=True)  
conn=db_engine.connect() 

query=pd.read_sql_query("select * from HIS_LITHIUM_CAN_DATA@orcl195",conn,chunksize=1000)

def ToDisk(filename):
    first=query.next()
    first.to_csv(filename,encoding="utf-8")
    for line in query:
        line.to_csv(filename,header=False,mode="a+",encoding="utf-8")

os.chdir('E:/work/data')
ToDisk('HIS_LITHIUM_CAN_DATA.csv')

参考资料

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0014021031294178f993c85204e4d1b81ab032070641ce5000

原文地址:https://www.cnblogs.com/sxbjdl/p/5842753.html