前言: 这篇博客主要介绍下SQLAlchemy及基本操作,写完后有空做个堡垒机小项目。有兴趣可看下python之数据库(mysql)操作。下篇博客整理写篇关于Web框架和django基础~~

一、ORM介绍

orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型的,为了保证一致的使用习惯,通过orm将编程语言的对象模型和数据库的关系模型建立映射关系,这样我们在使用编程语言对数据库进行操作的时候可以直接使用编程语言的对象模型进行操作就可以了,而不用直接使用sql语言。

orm的优点:

  • 隐藏了数据访问细节,“封闭”的通用数据库交互,ORM的核心。他使得我们的通用数据库交互变得简单易行,并且完全不用考虑该死的SQL语句。快速开发,由此而来。
  • ORM使我们构造固化数据结构变得简单易行。

缺点:

  • 无可避免的,自动化意味着映射和关联管理,代价是牺牲性能(早期,这是所有不喜欢ORM人的共同点)。现在的各种ORM框架都在尝试使用各种方法来减轻这块(LazyLoad,Cache),效果还是很显著的。

二、SQLAlchemy框架与数据库API

在Python中,最有名的ORM框架是SQLAlchemy。用户包括openstack\Dropbox等知名公司或应用,主要用户列表http://www.sqlalchemy.org/organizations.html#openstack

需要自己把数据库中的表映射成类,然后才能通过对象的方式去调用。SQLAlchemy不止可以支持MYSQL,还可以支持Oracle等。

Dialect用于和数据API进行交流,根据配置文件的不同调用不同的数据库API,从而实现对数据库的操作:

MySQL-Python
    mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>
  
pymysql
    mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
  
MySQL-Connector
    mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
  
cx_Oracle
    oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...]
  
更多详见:http://docs.sqlalchemy.org/en/latest/dialects/index.html

安装SQLAlchemy:

pip install SQLAlchemy

三、连接数据库并查询

 from sqlalchemy import create_engine

 #连接数据库,生成engine对象;最大连接数为5个
engine = create_engine("mysql+pymysql://root:root@127.0.0.1:3306/zcl", max_overflow=5)
print(engine) #Engine(mysql+pymysql://root:***@127.0.0.1:3306/zcl)
result = engine.execute('select * from students') #不用commit(),会自动commit
print(result.fetchall())

输出:

Engine(mysql+pymysql://root:***@127.0.0.1:3306/zcl)
[(, 'zcl', 'man', , '', None), (, 'alex', 'man', , '', None), (, 'Jack', 'man', , '', 'CN'), (, 'Mary', 'female', , '', 'USA'), (, 'Jack', 'man', , '', 'CN'), (, 'Jack2', 'man', , '', 'CN'), (, 'Mary', 'female', , '', 'USA'), (, 'cjy', 'man', , '', 'USA'), (, 'cjy2', 'man', , '', 'USA'), (, 'cjy3', 'man', , '', 'USA'), (, 'cjy4', 'man', , '', 'USA'), (, 'cjy5', 'man', , '', 'USA')]

四、创建表

创建user与color表: 创建表时需要与MetaData的实例绑定。

 from sqlalchemy import create_engine, \
Table, Column, Integer, String, MetaData, ForeignKey metadata = MetaData() #相当于实例一个父类 user = Table('user', metadata, #相当于让Table继承metadata类
Column('id', Integer, primary_key=True),
Column('name', String(20)),
) color = Table('color', metadata, #表名color
Column('id', Integer, primary_key=True),
Column('name', String(20)),
)
engine = create_engine("mysql+pymysql://root:root@localhost:3306/zcl", max_overflow=5) metadata.create_all(engine) #table已经与metadate绑定

查看创建的表:

五、增删改查

1. 先来了解下原生sql语句的增删改查:

 from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, ForeignKey,select

 metadata = MetaData()

 user = Table('user', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(20)),
) color = Table('color', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(20)),
)
engine = create_engine("mysql+pymysql://root:root@127.0.0.1:3306/zcl", max_overflow=5) conn = engine.connect() #创建游标,当前实例所处状态 # 创建SQL语句,INSERT INTO "user" (id, name) VALUES (:id, :name)
#id号可省略,默认是自增的
# conn.execute(user.insert(), {'id': 1, 'name': 'zcl'})
# conn.close() # sql = user.insert().values(name='wu') #插入
# conn.execute(sql)
# conn.close() #删除id号大于1的行,也可以where(user.c.name=="zcl")
# sql = user.delete().where(user.c.id > 1)
# conn.execute(sql)
# conn.close() # 将name=="wuu"更改为"name=="ed"
# sql = user.update().where(user.c.name == 'wuu').values(name='ed')
# conn.execute(sql)
# conn.close() #查询  下面不能写 sql = user.select... 会曝错
#sql = select([user, ]) #[(1, 'zcl'), (9, 'ed'), (10, 'ed')]
# sql = select([user.c.id, ]) #[(1,), (9,), (10,)]
sql = select([user.c.name, color.c.name]).where(user.c.id==color.c.id)
# sql = select([user.c.name]).order_by(user.c.name)
# sql = user.select([user]).group_by(user.c.name) result = conn.execute(sql)
print(result.fetchall())
conn.close()

2. 通过SQLAlchemy的增删改查(重要):

 from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Integer,String
from sqlalchemy.orm import sessionmaker Base = declarative_base()    #生成一个SqlORM基类(已经封装metadata)
#echo=True可以查看创建表的过程
engine = create_engine("mysql+pymysql://root:root@localhost:3306/zcl", echo=True) class Host(Base):
__tablename__ = 'hosts'   #表名为host
id = Column(Integer, primary_key=True, autoincrement=True)
hostname = Column(String(64), unique=True, nullable=False)
ip_addr = Column(String(128), unique=True, nullable=False)
port = Column(Integer, default=22) Base.metadata.create_all(engine)   #创建所有表结构 if __name__ == '__main__':
#创建与数据库的会话sessionclass,注意,这里返回给session的是个class类,不是实例
SessionCls=sessionmaker(bind=engine)
session=SessionCls()  #连接的实例
#准备插入数据
h1 = Host(hostname='localhost', ip_addr='127.0.0.1')   #实例化(未创建)
h2 = Host(hostname='ubuntu', ip_addr='192.168.2.243', port=20000) #session.add(h1)   #也可以用下面的批量处理
#session.add_all([h1,h2])
#h2.hostname='ubuntu_test'  #只要没提交,此时修改也没问题 #查询数据,返回一个对象
obj = session.query(Host).filter(Host.hostname=="localhost").first()
print("-->",obj)
#[<__main__.Hostobjectat0x00000000048DC0B8>]如果上面为.all()
#<__main__.Hostobjectat0x000000000493C208>如果上面为.first() #如果用.all(),会曝错AttributeError:'list'objecthasnoattribute'hostname'
#obj.hostname = "localhost_1"   #将主机名修改为localhost_1 session.delete(obj) #删除行 session.commit()#提交

操作结果截图:

六、外键关联

1. 创建主机表hosts与分组表group,并建立关联,即一个组可对应多个主机:

 from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String,ForeignKey
from sqlalchemy.orm import sessionmaker,relationship Base = declarative_base() # 生成一个SqlORM 基类(已经封闭metadata)
#echo=True可以查看创建表的过程
engine = create_engine("mysql+pymysql://root:root@localhost:3306/zcl", echo=True) class Host(Base):
__tablename__ = 'hosts' #表名
id = Column(Integer, primary_key=True, autoincrement=True) #默认自增
hostname = Column(String(64), unique=True, nullable=False)
ip_addr = Column(String(128), unique=True, nullable=False)
port = Column(Integer, default=22)
#外键关联,主机与组名关联,一个组对应多个主机
group_id = Column(Integer, ForeignKey("group.id")) class Group(Base):
__tablename__ = "group"
id = Column(Integer,primary_key=True)
name = Column(String(64), unique=True, nullable=False) Base.metadata.create_all(engine) # 创建所有表结构 if __name__ == '__main__':
# 创建与数据库的会话session class ,注意,这里返回给session的是个class,不是实例
SessionCls = sessionmaker(bind=engine)
session = SessionCls() #连接的实例 session.commit() #提交

查看结果:

问题: 查看新建的group表结构或从group表查询会发现desc group;select * from group都会曝错!!(为什么会产生这种错误可能是group与数据库有某些关联导致的,eg:group by... 我猜的)

解决方法: 用desc zcl.group; select * from zcl.group;  (zcl为数据库名)

2. 创建完表后就要在表中创建数据啦。接下来在hosts表与group表创建数据:

 from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String,ForeignKey
from sqlalchemy.orm import sessionmaker Base = declarative_base() # 生成一个SqlORM 基类(已经封闭metadata)
#echo=True可以查看创建表的过程
engine = create_engine("mysql+pymysql://root:root@localhost:3306/zcl", echo=True) class Host(Base):
__tablename__ = 'hosts' #表名
id = Column(Integer, primary_key=True, autoincrement=True) #默认自增
hostname = Column(String(64), unique=True, nullable=False)
ip_addr = Column(String(128), unique=True, nullable=False)
port = Column(Integer, default=22)
#外键关联,主机与组名关联
group_id = Column(Integer, ForeignKey("group.id")) class Group(Base):
__tablename__ = "group"
id = Column(Integer,primary_key=True)
name = Column(String(64), unique=True, nullable=False) Base.metadata.create_all(engine) # 创建所有表结构 if __name__ == '__main__':
# 创建与数据库的会话session class ,注意,这里返回给session的是个class,不是实例
SessionCls = sessionmaker(bind=engine)
session = SessionCls() #连接的实例 g1 = Group(name = "g1")
g2 = Group(name = "g2")
g3 = Group(name = "g3")
g4 = Group(name = "g4")
session.add_all([g1,g2,g3,g4]) #此时上面的g1,g2,g3三条记录还未存在,因为程序运行到这一行时还未commit(),故g1.id也未存在,但是下面一行代码是用到g1.id的!!经过测试: 运行时虽然不曝错,但关联不成功,如下图
h1 = Host(hostname='localhost', ip_addr='127.0.0.1',group_id=g1.id)
session.add(h1) session.commit() #提交

经过测试: 运行时虽然不曝错,但关联不成功,如下图:

3. 现在问题又来了,hosts表中的group_id可是为空啊!! 这肯定不行的。现在如何在不删除hosts表数据的前提下,使group_id不为空(eg: 使group_id为4,与g4建立关联)??可用下面的代码:

g4 = session.query(Group).filter(Group.name=="g4").first()     #找到g4组的对象
h = session.query(Host).filter(Host.hostname=="localhost").update({"group_id":g4.id}) #更新(修改)
session.commit() #提交

4. 问题: 如何获取与主机关联的group_id??

g4=session.query(Group).filter(Group.name=="g4").first()
h=session.query(Host).filter(Host.hostname=="localhost").first()
print("h1:",h.group_id)

好吧,我承认这个问题太简单了,通过上面的代码,找到主机的对象h, 则h.group_id就是答案。接下来的问题才是重点。

5. 此时可以获取已经关联的group_id,但如何获取已关联的组的组名??

print(h.group.name) #AttributeError:'Host'object has no attribute 'group'

嗯,你是初学者,你当然会说通过过h.group.name就可以找到与主机关联的组名! BUT,这是不行的,会曝错,因为Host类根本就没有group属性!!

解决方法:

  • first:
from sqlalchemy.orm import relationship       #导入relationship
  • second:

在Host类中加入group = relationship("Group"):

class Host(Base):
__tablename__ = 'hosts' #表名
id = Column(Integer, primary_key=True, autoincrement=True) #默认自增
hostname = Column(String(64), unique=True, nullable=False)
ip_addr = Column(String(128), unique=True, nullable=False)
port = Column(Integer, default=22)
#外键关联,主机与组名关联
group_id = Column(Integer, ForeignKey("group.id"))
group = relationship("Group")

此时再用print(h.group.name)就不会曝错啦!!

6. 哈哈,问题还没完呢。 前面已经实现:通过主机可查看对应组名,那么如何实现通过组名查看对应的主机??

经过前面5个点的历练,你已成为小小的老司机了,于是你很自信地说: 和第5个点一样,在Group类中加入hosts = relationship("Host");

 class Host(Base):
__tablename__ = 'hosts' #表名
id = Column(Integer,primary_key=True, autoincrement=True) #默认自增
hostname = Column(String(64), unique=True, nullable=False)
ip_addr = Column(String(128), unique=True, nullable=False)
port = Column(Integer, default=22)
#外键关联,主机与组名关联
group_id = Column(Integer,ForeignKey("group.id"))
group = relationship("Group") class Group(Base):
__tablename__ = "group"
id = Column(Integer, primary_key=True)
name = Column(String(64), unique=True, nullable=False)
hosts = relationship("Host") Base.metadata.create_all(engine) #创建所有表结构 g4 = session.query(Group).filter(Group.name=="g4").first()
h = session.query(Host).filter(Host.hostname=="localhost").first()
print("h1:",h.group_id) #h1: 4
#此时可以获取已经关联的group_id,但如何获取已关联的组的组名
print(h.group.name) #g4
print("g4:",g4.hosts) #g4:[<__main__.Hostobjectat0x0000000004303860>]

7. 通过上面的两句代码可实现双向关联。但必须在两个表都加上一句代码才行,有没有办法只用一句代码就实现双向关联?? 当然有,老司机会这么做:

在Host类中加入下面这句代码,即可实现双向关联:

group=relationship("Group",backref="host_list")

八、合并查询join

合并查询分为: inner join、left outer join、right outer join、full outer join

下面的例子可以让你完全理解join: http://stackoverflow.com/questions/38549/what-is-the-difference-between-inner-join-and-outer-join

关于join的原生sql操作:

在SQLAlchemy实现sql.join:

obj = session.query(Host).join(Host.group).all()    #相当于inner join
print("-->obj:",obj)

九、分类聚合group by

group by是啥意思呢? 我说下我的理解吧,group即分组,by为通过;合起来即: 通过XX分组;

举个例子吧,现在有两张表,分别是主机表与分组表。两表已经通过group_id建立关联,分组表中有4个数据,分别为g1,g2,g3,g4; id分别为1,2,3,4; 而主机表有3个数据,group_id分别为4,3,4; id分别为1,2,4; 现在对hosts表执行group by命令,进行分类聚合。

具体请看下图:

对应SQLAlchemy语句:

obj1 = session.query(Host).join(Host.group).group_by(Group.name).all()    #分类聚合
print("-->obj1:",obj1)

对应SQLAlchemy语句:

obj2 = session.query(Host,func.count(Group.name)).join(Host.group).group_by(Group.name).all()
print("-->obj2:",obj2) 输出:
-->obj2: [(<__main__.Host object at 0x0000000003C854A8>, 1), (<__main__.Host object at 0x0000000003C85518>, 2)]

十、多对多关联

多对多关联,即: 一个主机h1可对应在多个组(g1,g2),一个组(g1)可对应多个主机(h1,h2)

想实现如下的多对多关联,需要一张中间表。Eg:
h1 g1
h1 g2
h2 g1 Host表
h1
h2
h3
Group表
g1
g2
g3 HostToGroup中间表(实现多对多关联,sqlalchemy也是这样实现的)
id host_id group_id
1 1 1
2 1 2
3 2 1

虽然有了中间表,但如果想查看一个组对应的所有主机名或者一个主机对应的所有组,还是需要Group/Host与中间表进行一系列的关联操作(join~), 但SqlAlchemy简化了关联操作!!

调用下面命令便会自动关联中间表:

Host.groups()  #查看一个主机对应所有组
Group.hosts()

SQLAlchemy是如何实现多对多关联的??

1. 建立中间表,关联其它两个表

 from sqlalchemy import create_engine,func,Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String,ForeignKey
from sqlalchemy.orm import sessionmaker,relationship Base = declarative_base() # 生成一个SqlORM 基类(已经封闭metadata)
#echo=True可以查看创建表的过程
engine = create_engine("mysql+pymysql://root:root@localhost:3306/zcl", echo=True) #直接创建表并返回表的实例 Host2Group主动关联Host与Group(被关联)
Host2Group = Table('host_to_group',Base.metadata,
Column('host_id',ForeignKey('host.id'),primary_key=True),
Column('group_id',ForeignKey('group.id'),primary_key=True),
#一个表为什么能创建两个主键(其实是两个列同时作为主键,非空且唯一)
#PRIMARY KEY (host_id, group_id),
)

2. 在Host表(或Group表)指定中间表的实例,加上backref就不用在Group表中指定

 #声明表的映射关系
class Host(Base):
__tablename__ = 'host' #表名
id = Column(Integer, primary_key=True, autoincrement=True) #默认自增
hostname = Column(String(64), unique=True, nullable=False)
ip_addr = Column(String(128), unique=True, nullable=False)
port = Column(Integer, default=22)
#外键关联,主机与组名关联
#group_id = Column(Integer, ForeignKey("group.id"))
groups = relationship("Group", #关联Group表
secondary = Host2Group, #关联第三方表
backref = "host_list") #双向关联,不用在Group类中再加这句代码 def __repr__(self):
return "<id=%s,hostname=%s,ip_addr=%s>" % (self.id,
self.hostname,
self.ip_addr)

3. 创建组与主机

 if __name__ == '__main__':
SessionCls = sessionmaker(bind=engine)
session = SessionCls()
"""
g1 = Group(name = "g1")
g2 = Group(name = "g2")
g3 = Group(name = "g3")
g4 = Group(name = "g4")
session.add_all([g1,g2,g3,g4])
"""
"""
h1 = Host(hostname="h1",ip_addr="10.1.1.1")
h2 = Host(hostname="h2",ip_addr="10.1.1.2",port=10000)
h3 = Host(hostname="h3",ip_addr="10.1.1.3",port=6666)
session.add_all([h1,h2,h3])
"""

4. 建立关联与查询

     """
groups = session.query(Group).all()
h1 = session.query(Host).filter(Host.hostname=="h1").first()
h1.groups = groups #将h1关联到所有的组
print("-->:",h1.groups)
h1.groups.pop() #删除一个关联
"""
h2 = session.query(Host).filter(Host.hostname=="h2").first()
#h2.groups = groups[1:-1] #将h2关联到组(2和3)
print("=======>h2.groups:",h2.groups)
#=======>h2.groups: [<__main__.Group object at 0x00000000044A3F98>,
# <__main__.Group object at 0x00000000044A3FD0>]
#加上__repr__()后,变为=======>h2.groups: [<id=2,name=g2>, <id=3,name=g3>] g1 = session.query(Group).first()
print("=======>g1:",g1.host_list)
#=======>g1: [<id=1,hostname=h1,ip_addr=10.1.1.1>]
session.commit()

测试截图:

查看表结构:

查看表内容:

查看第三方表:

完整例子:

 from sqlalchemy import create_engine,func,Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String,ForeignKey
from sqlalchemy.orm import sessionmaker,relationship Base = declarative_base() # 生成一个SqlORM 基类(已经封闭metadata)
#echo=True可以查看创建表的过程
engine = create_engine("mysql+pymysql://root:root@localhost:3306/zcl", echo=True) #直接创建表并返回表的实例 Host2Group主动关联Host与Group(被关联)
Host2Group = Table('host_to_group',Base.metadata,
Column('host_id',ForeignKey('host.id'),primary_key=True),
Column('group_id',ForeignKey('group.id'),primary_key=True),
#一个表为什么能创建两个主键(其实是两个列同时作为主键,非空且唯一)
#PRIMARY KEY (host_id, group_id),
) #声明表的映射关系
class Host(Base):
__tablename__ = 'host' #表名
id = Column(Integer, primary_key=True, autoincrement=True) #默认自增
hostname = Column(String(64), unique=True, nullable=False)
ip_addr = Column(String(128), unique=True, nullable=False)
port = Column(Integer, default=22)
#外键关联,主机与组名关联
#group_id = Column(Integer, ForeignKey("group.id"))
groups = relationship("Group", #关联Group表
secondary = Host2Group, #关联第三方表
backref = "host_list")#双向关联,不用在Group类中再加这句代码 def __repr__(self):
return "<id=%s,hostname=%s,ip_addr=%s>" % (self.id,
self.hostname,
self.ip_addr) class Group(Base):
__tablename__ = "group"
id = Column(Integer,primary_key=True)
name = Column(String(64), unique=True, nullable=False) def __repr__(self):
return "<id=%s,name=%s>" % (self.id, self.name) Base.metadata.create_all(engine) # 创建所有表结构 if __name__ == '__main__':
SessionCls = sessionmaker(bind=engine)
session = SessionCls()
"""
g1 = Group(name = "g1")
g2 = Group(name = "g2")
g3 = Group(name = "g3")
g4 = Group(name = "g4")
session.add_all([g1,g2,g3,g4])
"""
"""
h1 = Host(hostname="h1",ip_addr="10.1.1.1")
h2 = Host(hostname="h2",ip_addr="10.1.1.2",port=10000)
h3 = Host(hostname="h3",ip_addr="10.1.1.3",port=6666)
session.add_all([h1,h2,h3])
"""
"""
groups = session.query(Group).all()
h1 = session.query(Host).filter(Host.hostname=="h1").first()
h1.groups = groups #将h1关联到所有的组
print("-->:",h1.groups)
h1.groups.pop() #删除一个关联
"""
h2 = session.query(Host).filter(Host.hostname=="h2").first()
#h2.groups = groups[1:-1]
print("=======>h2.groups:",h2.groups)
#=======>h2.groups: [<__main__.Group object at 0x00000000044A3F98>,
# <__main__.Group object at 0x00000000044A3FD0>]
#加上__repr__()后,变为=======>h2.groups: [<id=2,name=g2>, <id=3,name=g3>] g1 = session.query(Group).first()
print("=======>g1:",g1.host_list)
#=======>g1: [<id=1,hostname=h1,ip_addr=10.1.1.1>]
session.commit()

转发注明出处: http://www.cnblogs.com/0zcl/p/6504696.html

python之SQLAlchemy ORM 上的更多相关文章

  1. python之SQLAlchemy ORM

    前言: 这篇博客主要介绍下SQLAlchemy及基本操作,写完后有空做个堡垒机小项目.有兴趣可看下python之数据库(mysql)操作.下篇博客整理写篇关于Web框架和django基础~~ 一.OR ...

  2. Python 9 sqlalchemy ORM

    一.ORM介绍: orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型 ...

  3. python 学习笔记十一 SQLALchemy ORM(进阶篇)

    SqlAlchemy ORM SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据A ...

  4. python 之路,Day11 (下)- sqlalchemy ORM

    python 之路,Day11 - sqlalchemy ORM   本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业 1. ORM ...

  5. Python SQLAlchemy ORM示例

    SQLAlchemy的是Python的SQL工具包和对象关系映射,给应用程序开发者提供SQL的强大功能和灵活性. 安装 pip install mysql-python pip install sql ...

  6. python 之路,Day11(上) - python mysql and ORM

    python 之路,Day11 - python mysql and ORM   本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 ...

  7. Sqlalchemy python经典第三方orm

    Ⅰ. 安装 pip install sqlalchemy Ⅱ. 起步链接 import time import threading import sqlalchemy from sqlalchemy ...

  8. Python sqlalchemy orm 多对多外键关联

    多对多外键关联 注:使用三张表进行对应关联 实现代码: # 创建3个表 配置外键关联 # 调用Column创建字段 加类型 from sqlalchemy import Table, Column, ...

  9. SQLAlchemy(1) -- Python的SQLAlchemy和ORM

    Python的SQLAlchemy和ORM(object-relational mapping:对象关系映射) web编程中有一项常规任务就是创建一个有效的后台数据库.以前,程序员是通过写sql语句, ...

随机推荐

  1. docker mac 安装并初始化GO环境

    mac 环境下,安装docker 下载链接:https://download.docker.com/mac/stable/Docker.dmg 下载完毕后,直接双击安装,下一步直到最后 创建docke ...

  2. 判断activity是否显示在界面上

    boolean result = false; ActivityManager am = (ActivityManager) context .getSystemService(Context.ACT ...

  3. PHP实现仿Google分页效果的分页函数

    本文实例讲述了PHP实现仿Google分页效果的分页函数.分享给大家供大家参考.具体如下: /** * 分页函数 * @param int $total 总页数 * @param int $pages ...

  4. 升级wamp5集成安装包 php5.2到php5.3

    平时xp下面都使用wamp5集成开发 但php的空间命名需要php5.3 才支持,而且公司系统大部分都使用5.3,很多函数与5.2是不同的 难的在xp下面手动安装,集成包使用很方便,配置,快捷键都很不 ...

  5. JDBC-ODBC桥连接方式操纵SQL数据库

    /**  * 功能:演示使用JDBC-ODBC桥连接方式操纵SQL数据库  * 作者:徐守威  * 操作步骤:  * 1.配置数据源  * 2.在程序中连接数据源  * 3.操作数据  */ pack ...

  6. jQuery插件开发详解

    我们该如何扩展jQuery呢?主要可以通过下面2个来扩展:$.extend 和 $.fn $.extend如果把jQuery当成一个类,$.extend相当于为该类添加了静态方法extend. < ...

  7. BNU Online Judge-34978-汉诺塔

    题目链接 http://www.bnuoj.com/bnuoj/problem_show.php?pid=34978 这题在比赛时AC了不过那时是根据测试数据  抱着来试一下的想法,没想就AC了,其实 ...

  8. IIS 启用w3wp.exe调试 没有找到w3wp进程

    必须条件: 在进程列表的下面,有个show processes in all sessions,把它勾上就能看到了 . VS中附加进程的方式调试IIS页面,以及设置断点无效问题解决 以前调试网站的时候 ...

  9. FMS4中的P2P功能

    在fms4以前Adobe只允许在stratus中才能使用p2p功能.令人高兴的是,在最新发布的fms4中,p2p功能已经集成进来了,这将给实时视频类的应用带来更高的效率,adobe这次很给力! 为了使 ...

  10. Oracle job调用存储过程

    在PL/SQL中的what值中直接写入存储过程的名称+“分号”: begin sys.dbms_job.submit(job => :job, what => 'del_ky_items; ...