Python 3.x之数据库框架Sqlalchemy操作SQlite
目前Python 3.x的操作数据库框架比较少,其中Sqlalchemy是一比较优秀的ORM数据持久化操作框架,
它的下载地址为:
http://www.sqlalchemy.org/download.html
目前最新版本为:0.7.3
它不太支持python 3.x,当安装的时候,即使用python setup.py install.会出现
SQLAlchemy includes C extensions which provide an extra speed boost for dealing with result sets. Currently, the
extensions are only supported on the 2.xx series of cPython, not Python 3 or Pypy.
它只是一个警告,并不影响使用,因为它有个2to3.py程序,当版本为python 3.x的时候,它会自动调用的。
安装好以后:
我写了两个测试例子进行测试:
other.py:
- from sqlalchemy import create_engine
- engine=create_engine('sqlite:///:memory:',echo=True)
- from sqlalchemy import Table,Column,Integer,String,MetaData,ForeignKey
- metadata=MetaData()
- users_table=Table('users',metadata,
- Column('id',Integer,primary_key=True),
- Column('name',String),
- Column('fullname',String),
- Column('password',String)
- )
- metadata.create_all(engine)
- class User(object):
- def __init__(self,name,fullname,password):
- self.name=name
- self.fullname=fullname
- self.password=password
- def __repr__(self):
- return "<User('%s','%s','%s')>" % (self.name,self.fullname,self.password)
- from sqlalchemy.orm import (mapper, relationship, sessionmaker)
- #print(User.__table__)
- #print(User.__mapper__)
- mapper(User,users_table)
- ed_user=User('ed','Ed Jones','edspassword')
- ed_user.password = 'f8s7ccs'
- print(ed_user.fullname)
- print(ed_user.password)
还有一个例子为:
- __author__ = 'minggxu9'
- from sqlalchemy import Table,Column,Integer,String,MetaData,ForeignKey
- from sqlalchemy.ext.declarative import declarative_base
- Base=declarative_base() #基类
- class User(Base):
- __tablename__='users' #表名
- id=Column(Integer,primary_key=True)
- name=Column(String)
- fullname=Column(String)
- password=Column(String)
- def __init__(self,name,fullname,password):
- self.name=name
- self.fullname=fullname
- self.password=password
- def __repr__(self):
- return "<User('%s','%s','%s')>" % (self.name,self.fullname,self.password)
- users_table=User.__table__ #用来获得Table
- metadata=Base.metadata #获得MetaDATA
- #创建Session并绑定一个数据库链接
- from sqlalchemy.orm import sessionmaker
- from sqlalchemy import engine
- Session=sessionmaker(bind=engine)
- #如果没有数据库链接,可以这样创建session
- Session=sessionmaker()
- #当后来由数据库链接后可以这样绑定
- Session.configure(bind=engine)
- ed_user = User('ed', 'Ed Jones', 'edspassword')
- session = Session()
- session.add(ed_user)
- #通过session的add方法添加一个对象
- from sqlalchemy import *
- #our_user = session.query(User).filter_by(name='ed').first()
- #print(ed_user is our_user )
- session.add_all([
- User('wendy', 'Wendy Williams','foobar'),
- User('mary', 'Mary Contrary', 'xxg527'),
- User('fred', 'Fred Flinstone','blah')])
- session.dirty
- session.new
- session.commit()
相关推荐
xiaoxiangyu 2020-02-23
liuweiq 2020-06-14
emmm00 2020-06-14
lt云飞扬gt 2020-05-29
朱建伟 2020-05-14
猛禽的编程艺术 2020-05-06
苦咖啡flask 2020-05-04
dreamhua 2020-04-30
dreamhua 2020-04-30
tanrong 2020-04-30
xuanlvhaoshao 2020-04-26
tanrong 2020-04-18
Danialzhou 2020-04-11
happinessaflower 2020-03-01
喝绿茶的猫 2020-01-18
疯狂老司机 2020-01-10
liuyang000 2020-01-08
Lingforme 2020-01-08