首页 > 代码库 > sqlalchemy - day1
sqlalchemy - day1
一、Create engine
Database url规则: dialect+driver://username:password@host:port/database
echo: True表示cmd窗口显示出对应的SQL 脚本信息
1 from sqlalchemy import create_engine 2 3 # Database url: dialect+driver://username:password@host:port/database 4 # eq: mysql+pymysql://purk:max123@local/test 5 # 在内存中创建一个sqllite 6 # engine = create_engine(‘sqlite:///:memory:‘, echo=True) 7 # F:/test.db 如果test.db不存在,则创建一个。 8 engine = create_engine(‘sqlite:///F:/test.db‘, echo=True)
二、create mapping class
1 from sqlalchemy import Column, Integer, String, Table 2 from sqlalchemy.ext.declarative import declarative_base 3 4 Base = declarative_base() 5 6 7 class Person(Base): 8 __tablename__ = ‘person‘ 9 10 id = Column(Integer, primary_key=True) 11 name = Column(String(50)) 12 13 class User(Base): 14 __tablename__=‘user‘ 15 16 id = Column(Integer, primary_key=True) 17 name = Column(String(50))
1 >>> Person.__table__ 2 Table(‘person‘, MetaData(bind=None), Column(‘id‘, Integer(), table=<person>, primary_key=True, nullable=False), Column(‘name‘, String(length=50), table=<person>), schema=None) 3 4 >>> Person.metadata is User.metadata 5 True 6 7 >>> Person.metadata is Base.metadata 8 True
传统的mapper configuration 就不介绍了,因为不直观,而且代码量还长,下面只贴出官网的例子
1 from sqlalchemy import Table, MetaData, Column, Integer, String, ForeignKey 2 from sqlalchemy.orm import mapper 3 metadata =http://www.mamicode.com/ MetaData() 4 user = Table(‘user‘, metadata, 5 Column(‘id‘, Integer, primary_key=True), 6 Column(‘name‘, String(50)), 7 Column(‘fullname‘, String(50)), 8 Column(‘password‘, String(12)) 9 ) 10 11 12 class User(object): 13 14 15 def __init__(self, name, fullname, password): 16 self.name = name 17 self.fullname = fullname 18 self.password = password 19 mapper(User, user)
三、Column
1. DataType
1) Integer
id = Column(Integer)
2) String
name = Column(String(50))
3) Boolean
gender = Column(Boolean)
1 person = Person(name=‘purk‘, gender=0) 2 person1 = Person(name=‘purk1‘, gender=11) 3 person2 = Person(name=‘purk2‘, gender=-1) 4 # try: 5 # person3 = Person(name=‘purk3‘, gender=‘123‘) 6 # except Exception as e: 7 # print(‘boolean类型在数据库中对应的smartint或boolean类型,输入字符串是不对的‘) 8 db.add_all([person, person1, person2]) 9 db.commit()
结果如下
1 person_1 = db.query(Person).filter(Person.name == ‘purk‘).first() 2 person_2 = db.query(Person).filter(Person.name == ‘purk1‘).first() 3 person_3 = db.query(Person).filter(Person.name == ‘purk2‘).first() 4 print(person_1.gender) 5 print(person_2.gender) 6 print(person_3.gender)
查询 结果如下,满足boolean类型的一贯判断,非0即1.
4) Date -> datetime.date()
5) DateTime -> datetime.datetime()
6) Enum
level_list = (‘low‘, ‘medium‘, ‘high‘)
level = Column(Enum(*level_list))
1 person = Person(name=‘purk‘, gender=True, level=‘123‘) 2 person1 = Person(name=‘purk1‘, gender=False, level=0) 3 person2 = Person(name=‘purk2‘, gender=False, level=1) 4 person3 = Person(name=‘purk3‘, gender=False, level=4) 5 person4 = Person(name=‘purk4‘, gender=-1, level=‘medium‘)
Enum的index是从1开始的,越界的或者值不在枚举列中的都保存为null了
sqlalchemy - day1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。