SQLAlchemy连接Oracle

前端之家收集整理的这篇文章主要介绍了SQLAlchemy连接Oracle前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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',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:password@192.168.1.6: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

猜你在找的Oracle相关文章