欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

python学习课程(九)sqlalchemy框架的modern映射

更新时间: 2014-01-05 02:07:58 责任编辑: Author_N1

 

python学习教程(九)sqlalchemy框架的modern映射

首先写一个modern.py文件,


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

'''the connection to the database'''
engine = create_engine("mysql://root:1933jld1945js@localhost/test",isolation_level="READ UNCOMMITTED") 
'''the construct of the database table'''
Base = declarative_base()

class People():
    id = Column(Integer, primary_key=True)
    name = Column(String(16))
    gender = Column(String(16))
    age = Column(String(16))

class User(Base,People):
    __tablename__ = 'users'

    fullname = Column(String(16))
    password = Column(String(16))

class AdvancedUser(Base,People):
    __tablename__ = 'advanceduser'
    
    fullname = Column(String(16))
    password = Column(String(16))
    email = Column(String(16))
    
class Teacher(Base,People):
    __tablename__ = 'teacher'
    
    phone = Column(String(16))
    
Base.metadata.create_all(engine)

然后写一个control.py文件,内容如下:

from sqlalchemy.orm.session import sessionmaker
from modern import engine,User,AdvancedUser,Teacher

user = User()
user.name = 'username'
user.gender = 'male'
au = AdvancedUser()
au.name = 'auname'
au.gender = 'female'
teacher = Teacher()
teacher.name = 'wangdi'
teacher.gender = 'female'
teacher.phone = '15264397652'
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
session.add(user)
session.add(au)
session.add(teacher)
session.flush()
session.commit()

运行control.py可以测试代码。

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:07:58
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/6004/
本文WWW.DOC100.NET DOC100.NET版权所有。