SQLAlchemy连接Oracle

为什么80%的码农都做不了架构师?>>>   hot3.png

SQLAlchemy是python下的数据库orm映射框架, 几乎支持所有常用数据库, 很受python开发者的青睐

SQLAlChemy依赖cx_oracle

先根据自己的python版本下载oracle库: cx_oracle

本地也要按照oracle的客户端, 可以到甲骨文官网下载到

# coding: utf-8

from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base		

Base = declarative_base()

class Post(Base):
	__tablename__ = 't_post'
	id     = Column('id', Integer, primary_key=True)
	date   = Column('date', String)
	post   = Column('post', String)
	name   = Column('name', String)
	value  = Column('value', String)
	time   = Column('time', String)

engine = create_engine('oracle://username:[email protected]:1521/databasename', echo=True)
Database= sessionmaker(bind=engine)

if __name__ == '__main__':
	db = Database()
	query = db.query(Post).filter(Post.name.like('%xxx'))
	print query.count()
	query = query.all()[30:40]
	for x in query:
		print x.id, x.name

 

 

 

转载于:https://my.oschina.net/yehun/blog/870947

你可能感兴趣的:(SQLAlchemy连接Oracle)