知乎: 使用 sqlalchemy 的 orm 方式運算元據庫是一種怎樣的體驗?
答: 酸爽!
這是 sqlalchemy 的層級圖。不難發現,其中 orm 是最頂級的封裝。
orm 基本操作步驟如下:
from sqlalchemy importcreate_engineengine = create_engine('sqlite:///:memory:', echo=true)
from sqlalchemy.orm importsessionsession = session(engine)
from sqlalchemy.ext.declarative importdeclarative_base
base = declarative_base()
from sqlalchemy importcolumn, integer, foreignkey
from sqlalchemy.orm import
relationship
class
parent(base):
__tablename__ = '
parent
'id = column(integer, primary_key=true)
children = relationship("
child")
def__repr__
(self):
return""
.format(self.id, self.children)
class
child(base):
__tablename__ = '
child
'id = column(integer, primary_key=true)
parent_id = column(integer, foreignkey('
parent.id'))
def__repr__
(self):
return
"".format(self.id, self.parent_id)
base.metadata.drop_all(engine)base.metadata.create_all(engine)
#生產環境只需執行一次!!!!
session.add_all([parent(children=[child() for j in range(5)]) for i in range(2)])session.commit()
#懶載入 (lazyload)
#load everything, no eager loading.
for parent in
session.query(parent):
(parent.children)
#聯合載入 (joinedload)
#load everything, joined eager loading.
for parent in session.query(parent).options(joinedload("
children
")):
parent.children
#子查詢載入 (subqueryload)
#load everything, subquery eager loading.
for parent in session.query(parent).options(subqueryload("
children
")):
parent.children
from sqlalchemy importcreate_engine
from sqlalchemy.orm import
session
from sqlalchemy.ext.declarative import
declarative_base
from sqlalchemy import
column, integer, foreignkey
from sqlalchemy.orm import
relationship
from sqlalchemy.orm import
joinedload, subqueryload
#建立連線
engine = create_engine('
sqlite:///:memory:
', echo=true)
#建立會話
session =session(engine)
#宣告基類
base =declarative_base()
#定義表(繼承基類)
class
parent(base):
__tablename__ = '
parent
'id = column(integer, primary_key=true)
children = relationship("
child")
def__repr__
(self):
return""
.format(self.id, self.children)
class
child(base):
__tablename__ = '
child
'id = column(integer, primary_key=true)
parent_id = column(integer, foreignkey('
parent.id'))
def__repr__
(self):
return""
.format(self.id, self.parent_id)
#生成表
base.metadata.drop_all(engine)
base.metadata.create_all(engine)
#生產環境只需執行一次!!!!
#新增記錄
session.add_all([parent(children=[child() for j in range(5)]) for i in range(2)])
session.commit()#查詢
#lazyload
#load everything, no eager loading.
for parent in
session.query(parent):
(parent.children)
#joinedload
#load everything, joined eager loading.
for parent in session.query(parent).options(joinedload("
children
")):
parent.children
#subqueryload
#load everything, subquery eager loading.
for parent in session.query(parent).options(subqueryload("
children
")):
parent.children
ORM框架SQLAlchemy的操作
1.首先建立乙個空py檔案 這裡命名為connect.py from sqlalchemy import create engine from sqlalchemy.ext.declarative import declarative base from sqlalchemy.orm import ...
ORM框架SQLAlchemy的操作
1.首先建立乙個空py檔案 這裡命名為connect.py from sqlalchemy import create engine from sqlalchemy.ext.declarative import declarative base from sqlalchemy.orm import ...
使用sqlalchemy的ORM建立外來鍵關聯時報錯
在學習使用sqlalchemy模組的時候踩了乙個坑,分享一下。我先用下面的語句建立了一張學生資訊表 create table student id int unsigned auto increment,name varchar 20 not null,age tinyint,primary key...