知乎: sqlalchemy 的 core 方式運算元據是一種怎樣的體驗?
答: 爽!
基本步驟如下:
from sqlalchemy importcreate_engine
engine = create_engine('
sqlite:///:memory:
', echo=true)
conn = engine.connect()
from sqlalchemy importmetadatametadata = metadata(engine)
from sqlalchemy importtable, column, integer, string, foreignkey, sequence
users = table('
users
', metadata,
column('id
', integer, sequence('
user_id_seq
'), primary_key=true),
column(
'name
', string),
column(
'fullname
', string),
)addresses = table('
addresses
', metadata,
column('id
', integer, primary_key=true),
column(
'user_id
', none, foreignkey('
users.id
')),
column(
'email_address
', string, nullable=false)
)
# metadata.drop_all()metadata.create_all()
#方式一ins = users.insert().values(name='
jack
', fullname='
jack jones')
conn.execute(ins)
#方式二
conn.execute(users.insert(), id=2, name='
wendy
', fullname='
wendy williams')
#方式三
conn.execute(addresses.insert(), [,,
,,
])
from sqlalchemy.sql importselect
for row in
conn.execute(select([users])):
print("
name:
", row[users.c.name], "
; fullname:
", row[users.c.fullname])
for row in
conn.execute(select([users, addresses])):
(row)
for row in conn.execute(select([users, addresses]).where(users.c.id ==addresses.c.user_id)):
(row)
from sqlalchemy.sql import
and_, or_, not_
s = select([(users.c.fullname +
", " +addresses.c.email_address).
label(
'title
')]).\
where(
and_(
users.c.id ==addresses.c.user_id,
users.c.name.between('m
', 'z'
), or_(
addresses.c.email_address.like(
'%@aol.com'),
addresses.c.email_address.like(
'%@msn.com')))
)conn.execute(s).fetchall()
#繫結資料庫
from sqlalchemy import
create_engine
engine = create_engine('
sqlite:///:memory:
', echo=true)
#連線資料庫
conn =engine.connect()
#元資料
from sqlalchemy import
metadata
metadata =metadata(engine)
#定義表
from sqlalchemy import
table, column, integer, string, foreignkey, sequence
users = table('
users
', metadata,
column('id
', integer, sequence('
user_id_seq
'), primary_key=true),
column(
'name
', string),
column(
'fullname
', string),
)addresses = table('
addresses
', metadata,
column('id
', integer, primary_key=true),
column(
'user_id
', none, foreignkey('
users.id
')),
column(
'email_address
', string, nullable=false))#
建立表# metadata.drop_all()
metadata.create_all()#插入
#方式一
ins = users.insert().values(name='
jack
', fullname='
jack jones')
result =conn.execute(ins)
#方式二
conn.execute(users.insert(), id=2, name='
wendy
', fullname='
wendy williams')
#方式三
conn.execute(addresses.insert(), [,,
,,
])#查詢from sqlalchemy.sql import
select
for row in
conn.execute(select([users])):
print("
name:
", row[users.c.name], "
; fullname:
", row[users.c.fullname])
for row in
conn.execute(select([users, addresses])):
(row)
for row in conn.execute(select([users, addresses]).where(users.c.id ==addresses.c.user_id)):
(row)
from sqlalchemy.sql import
and_, or_, not_
s = select([(users.c.fullname +
", " +addresses.c.email_address).
label(
'title
')]).\
where(
and_(
users.c.id ==addresses.c.user_id,
users.c.name.between('m
', 'z'
), or_(
addresses.c.email_address.like(
'%@aol.com'),
addresses.c.email_address.like(
'%@msn.com')))
)conn.execute(s).fetchall()
tornado框架SQLAlchemy的操作
在用到python的一些框架裡,需要進行前後端資料互動,其中資料庫的連線是必不可少的,之前自己使用sqlalchemy在進行資料庫連線時遇到了很多問題,以及一些流程不是很清楚。所以花了點時間進行了一下流程梳理。1.首先建立乙個空py檔案 這裡命名為connect.py 匯入包 from sqlalc...
sqlalchemy的初步認識
sqlalchemy是python的乙個包,用來運算元據庫.其實sqlalchemy自己並不能連線資料庫,它也是通過pymysql或者其他資料庫連線工具連線的.alchemy,煉丹術,煉金術,魔法 通過sqlalchemy可以使不同的 通過不同的工具連線不同的資料庫.就像是煉丹爐一樣,一切操作都在這...
Sqlalchemy中的事務
from flask import flask from flask sqlalchemy import sqlalchemy class user db.model id db.column db.integer,primary key true name db.column db.string ...