轉載自:http://snoopyxdy.blog.163.com/blog/static/601174402014770595787/
接上一節,繼續SQLAlchemy的學習心得,本章介紹直接使用table的增删改查操作,還不是orm哦。
一、插入操作
from
demo_define_table
import
user_table
#第一種插入方式
stmt = user_table.insert(values=dict(user_name=u'富士達1', password=u'富士達1', email_address=u'Rick 富士達1'))
stmt.execute()
#第二種插入方式
stmt = user_table.insert()
stmt.execute(user_name=u'富士達', password=u'富士達', email_address=u'Rick 富士達')
我們從 demo_define_table檔案擷取到定義的 user_table 表類型,然後直接就可以進行insert操作
二、更新操作
#第一種方式更新
stmt = user_table.update(
whereclause=text("user_name='富士達'"),
values=dict(password=u'passwd'))
stmt.execute()
#第二種方式更新
stmt = user_table.update(
text("user_name='富士達1'"))
stmt.execute(password=u'passwd1')
也可以從 user_table 執行個體進行update操作,下面是一個子查詢的update操作,将 product_price_table 表内所有 sku 字段和 product_table 表内sku字段相同的,更新成 product_table表的 msrp字段值
msrp = select(
[product_table.c.msrp],
product_table.c.sku==product_price_table.c.sku,
limit=1)
stmt = product_price_table.update( values=dict(price=msrp))
stmt.execute()
三、删除操作
stmt
=
user_table
.
delete
(
text
(
"user_name='富士達'"
))
stmt
.
execute
()
删除操作比較簡單,給一個查詢條件即可
四、查詢 查詢是SQL語句中最為複雜的也是最為強大的,我們可以使用select函數和select方法,兩者是等價的:
#使用select函數
stmt = select([user_table.c.user_name])
for row in stmt.execute():
print row
#使用select方法
stmt = user_table.select()
for row in stmt.execute():
print row
1、select參數: 1、columns=None,表示要查找的列名 2、bind=None,資料庫引擎,如果忽略這個設定,将會使用本表的綁定引擎 3、whereclause(whereclause)=None,where的條件, 4、from_obj=[],設定from的條件,如果忽略它,那麼将會由SQLAlchemy自動根據其他條件得出 5、order_by=None,排序條件 6、group_by=None,分組條件 7、having=None,having要素 8、distinct=False,增加distinct篩選 9、for_update=False,增加一個FOR UPDATE篩選,像mysql可以使用‘read’用來鎖住表,保證接下來的update準确性 10、limit=None,限制行數 11、offset=None,偏移量 12、correlate=True,是否關聯查詢 13、use_labels=False,對列名數組生成唯一的标簽,防止列名沖突 14、prefixes=None,将一數組的指令插入到SELECT關鍵字後和列名前
2、where子句 用來建構where子句,我們可以使用text或者SQL 表達式文法。最簡單的方法用來生成where子句就是使用SQLAlchemy提供的操作
stmt
=
user_table
.
select
(
user_table
.
c
.
user_name
==
u
"富士達1"
)
print
stmt
.
execute
().
fetchall
()
where操作符
>>>
print
product_price_table
.
c
.
price
==
12.34
product
_price
.
price
=
?
>>>
print
product_price_table
.
c
.
price
!=
12.34
product
_price
.
price
!=
?
>>>
print
product_price_table
.
c
.
price
<
12.34
product
_price
.
price
<
?
>>>
print
product_price_table
.
c
.
price
>
12.34
product
_price
.
price
>
?
>>>
print
product_price_table
.
c
.
price
<=
12.34
product
_price
.
price
<=
?
>>>
print
product_price_table
.
c
.
price
>=
12.34
product
_price
.
price
>=
?
>>>
print
product_price_table
.
c
.
price
==
None
product_price
.
price IS NULL
3、AND OR NOT 與操作,或操作,非操作
print
(
product_table
.
c
.
msrp
>
10.00
)
&
(
product_table
.
c
.
msrp
<
20.00
)
print
and_
(
product_table
.
c
.
msrp
>
10.00
,
product_table
.
c
.
msrp
<
20.00
)
print
product_table
.
c
.
sku
.
like
(
'12%'
)
print
not_
(
and_
(
product_table
.
c
.
msrp
>
10.00
,
product_table
.
c
.
msrp
<
20.00
))
4、其他條件操作符 #其他條件操作符 between(cleft, cright),在這個區間内 distinct(),distinct操作 startswith(other),就像'other%' endswith(other),就像'%other' in_(*other),也可以是子查詢,或者一個數組 like(other),like操作 op(operator),操作 label(name),别名,相當于給列 as 名字
5、綁定查詢條件 可以加速查詢,簡潔代碼,例子如下:
stmt
=
select
([
product_table
.
c
.
msrp
],
whereclause
=
product_table
.
c
.
sku
==
bindparam
(
'sku'
))
print
stmt
.
execute
(
sku
=
'123'
).
fetchall
()
[(
12.34
,)]
print
stmt
.
execute
(
sku
=
'456'
).
fetchall
()
[(
22.120000000000001
,)]
print
stmt
.
execute
(
sku
=
'789'
).
scalar
()
41.44
bindparam函數的參數說明 1、key,綁定的列名 2、value=None,預設列名的值 3、type=None,綁定的類型 4、shortname=None,列名别名 5、unique=False,産生所綁定的列名是唯一名稱,通常伴随 value 參數一起使用
6、使用字元串條件
stmt
=
select
([
'product.msrp'
],
from_obj
=[
'product'
],
whereclause
=
text
(
"product.sku=='123'"
))
7、排序order,分組groupby和消除重複distinct 1、排序
stmt
=
product_table
.
select
(
order_by
=[
product_table
.
c
.
msrp
])
stmt
=
product_table
.
select
(
order_by
=[
desc
(
product_table
.
c
.
msrp
)])
2、groupby 然後 count
stmt
=
select
([
product_price_table
.
c
.
sku
,
func
.
count
(
product_price_table
.
c
.
store_id
)],
group_by
=[
product_price_table
.
c
.
sku
])
另外一個having的例子
stmt
=
select
([
product_price_table
.
c
.
sku
,
func
.
count
(
product_price_table
.
c
.
store_id
)],
group_by
=[
product_price_table
.
c
.
sku
],
having
=
func
.
count
(
product_price_table
.
c
.
store_id
)
3、distinct的例子
stmt
=
select
([
product_price_table
.
c
.
sku
,
product_price_table
.
c
.
price
],
distinct
=
True
)
8、limit和offset 分頁使用,limit和offset的例子
stmt = product_table.select(offset=1, limit=1)
9、多表聯合查詢 1、建立from的對象
from_obj
=
store_table
.
join
(
product_price_table
)
.
join
(
product_table
)
query
=
store_table
.
select
()
query
=
query
.
select_from
(
from_obj
)
query
=
query
.
where
(
product_table
.
c
.
msrp
!=
product_price_table
.
c
.
price
)
print
query
#列印資訊
SELECT store.id, store.name
FROM store JOIN product_price ON store.id = product_price.store_id
JOIN product ON product.sku = product_price.sku
WHERE product.msrp != product_price.price
将多表查詢整合在一句裡面的例子:
query2
=
select
([
store_table
,
product_table
.
c
.
sku
],
from_obj
=[
from_obj
],
whereclause
=(
product_table
.
c
.
msrp
!=
product_price_table
.
c
.
price
))
print
query2
#列印資訊
SELECT store.id, store.name, product.sku
FROM store JOIN product_price ON store.id = product_price.store_id
JOIN product ON product.sku = product_price.sku
WHERE product.msrp != product_price.price
outerjoin的例子
from_obj
=
store_table
.
outerjoin
(
product_price_table
)
from_obj
=
from_obj
.
outerjoin
(
product_table
)
query
=
store_table
.
select
()
query
=
query
.
select_from
(
from_obj
)
query
=
query
.
column
(
'product.msrp'
)
print
query
#列印資訊
SELECT store.id, store.name, product.msrp
FROM store LEFT OUTER JOIN product_price
ON store.id = product_price.store_id
LEFT OUTER JOIN product
ON product.sku = product_price.sku
10、set 操作符 UNION, INTERSECT, EXCEPT union(), union_all(), intersect(), intersect_all(), except_(), and except_all() 下面2個語句是等價的
query
=
product_table
.
select
(
and_
(
product_table
.
c
.
msrp
>
10.00
,
product_table
.
c
.
msrp
<
20.00
))
query0
=
product_table
.
select
(
product_table
.
c
.
msrp
>
10.00
)
query1
=
product_table
.
select
(
product_table
.
c
.
msrp
<
20.00
)
query
=
intersect
(
query0
,
query1
)
11、Subqueries 子查詢 in_操作符
subquery
=
select
([
employee_table
.
c
.
id
],
employee_table
.
c
.
manager_id
==
None
)
stmt
=
employee_table
.
select
(
and_
(
employee_table
.
c
.
id
.
in_
(
subquery
),
employee_table
.
c
.
name
.
like
(
'Ted%'
)))
下一節将真正的學習SQLAlchemy的對象關系映射技巧