增加crud的学习

main
roger 1 year ago
parent b00e52b686
commit 33b245d70f
  1. 64
      sqlalchemy_1x/data_prepare.py
  2. 0
      sqlalchemy_1x/main.py
  3. 2
      sqlalchemy_1x/s1_models.py
  4. 0
      sqlalchemy_1x/s2.1_insert_data.py
  5. 0
      sqlalchemy_1x/s2.2_fetch_data.py
  6. 69
      sqlalchemy_1x/s2.3_filter_data.py
  7. 14
      sqlalchemy_1x/s2.4_update_data.py
  8. 6
      sqlalchemy_1x/s2.5_delete_data.py
  9. 21
      sqlalchemy_1x/s2.6_join.py

@ -0,0 +1,64 @@
from sqlalchemy import insert
from s1_models import *
customer_list = [
{
'username': 'cookiemon',
'email_address': 'mon@cookie.com',
'phone': '111-111-1111',
'password': 'password'
},
{
'username': 'cakeeater',
'email_address': 'cakeeater@cake.com',
'phone': '222-222-2222',
'password': 'password'
},
{
'username': 'pieguy',
'email_address': 'guy@pie.com',
'phone': '333-333-3333',
'password': 'password'
}
]
ins = users.insert()
result = connection.execute(ins, customer_list)
ins = insert(orders).values(user_id=1, order_id=1)
result = connection.execute(ins)
ins = insert(line_items)
order_items = [
{
'order_id': 1,
'cookie_id': 1,
'quantity': 2,
'extended_cost': 1.00
},
{
'order_id': 1,
'cookie_id': 3,
'quantity': 12,
'extended_cost': 3.00
}
]
result = connection.execute(ins, order_items)
ins = insert(orders).values(user_id=2, order_id=2)
result = connection.execute(ins)
ins = insert(line_items)
order_items = [
{
'order_id': 2,
'cookie_id': 1,
'quantity': 24,
'extended_cost': 12.00
},
{
'order_id': 2,
'cookie_id': 4,
'quantity': 6,
'extended_cost': 6.00
}
]
result = connection.execute(ins, order_items)

@ -25,7 +25,7 @@ orders = Table('orders', metadata,
Column('order_id', Integer(), primary_key=True), Column('order_id', Integer(), primary_key=True),
Column('user_id', ForeignKey('users.user_id')), Column('user_id', ForeignKey('users.user_id')),
Column('shipped', Boolean(), default=False) Column('shipped', Boolean(), default=False)
) )
line_items = Table('line_items', metadata, line_items = Table('line_items', metadata,
Column('line_items_id', Integer(), primary_key=True), Column('line_items_id', Integer(), primary_key=True),
Column('order_id', ForeignKey('orders.order_id')), Column('order_id', ForeignKey('orders.order_id')),

@ -0,0 +1,69 @@
from s1_models import *
from sqlalchemy import select
# 一般查询
s1 = select([cookies]).where(cookies.c.cookie_name == 'chocolate chip')
rp1 = connection.execute(s1)
record1 = rp1.first()
print(record1.items())
# Like查询
s2 = select([cookies]).where(cookies.c.cookie_name.like('%chocolate%'))
rp2 = connection.execute(s2)
record2 = rp2.fetchall()
for r in record2:
print(r.cookie_name)
# ClauseElement
# between(cleft, cright) 查找在 cleft 和 cright 之间的列
# concat(column_two) 连接列
# distinct() 查找列的唯一值
# in_([list]) 查找列在列表中的位置
# is_(None) 查找列 None 的位置(通常用于检查 Null 和 None)
# contains(string) 查找包含 string 的列(区分大小写)
# endswith(string) 查找以 string 结尾的列(区分大小写)
# like(string) 查找与 string 匹配的列(区分大小写)
# startswith(string) 查找以 string 开头的列(区分大小写)
# ilike(string) 查找与 string 匹配的列(不区分大小写)
# between
# s3 = select([cookies]).where(cookies.c.quantity.between(10, 99)) # between
# s3 = select([cookies.c.cookie_name.concat(cookies.c.cookie_sku)]) # concat
# s3 = select([cookies.c.cookie_name.distinct()]) # distinct
# s3 = select([cookies]).where(cookies.c.cookie_sku.in_(['CC01', 'CC02'])) # in_
# s3 = select([cookies.c.cookie_id]).where(cookies.c.cookie_recipe_url.is_(None)) # is_(None)
# s3 = select([cookies]).where(cookies.c.cookie_sku.contains('01')) # contains
# s3 = select([cookies]).where(cookies.c.cookie_name.endswith('chip')) # endswith
# s3 = select([cookies]).where(cookies.c.cookie_sku.startswith('CC')) # startswith
s3 = select([cookies]).where(cookies.c.cookie_sku.ilike('%cc%')) # ilike
rp3 = connection.execute(s3)
record3 = rp3.fetchall()
for r in record3:
print(r)
#
# # 运算符
# s4 = select([cookies.c.cookie_name, 'sku-' + cookies.c.cookie_sku])
# rp4 = connection.execute(s4)
# for r in rp4.fetchall():
# print(r)
#
# # Cast
# from sqlalchemy import cast
#
# s5 = select([cookies.c.cookie_name, cast((cookies.c.quantity * cookies.c.unit_cost), Numeric(12, 2)).label('cost')])
# rp5 = connection.execute(s5)
# for r in rp5.fetchall():
# print(r.cost)
#
# # or and not
# from sqlalchemy import and_, or_, not_
#
# s6 = select([cookies]).where(and_(cookies.c.quantity > 10, cookies.c.unit_cost < 0.40))
# for r in connection.execute(s6):
# print(r)
#
# s7 = select([cookies]).where(or_(cookies.c.quantity.between(10, 99), cookies.c.cookie_name.contains('chip')))
# rp7 = connection.execute(s7)
# for r in rp7.fetchall():
# print(r)

@ -0,0 +1,14 @@
from sqlalchemy import update, select
from s1_models import *
# update
u = update(cookies).where(cookies.c.cookie_name == "chocolate chip").values(quantity=(cookies.c.quantity + 2))
result = connection.execute(u)
print(result.rowcount)
print(result.last_updated_params())
s = select([cookies]).where(cookies.c.cookie_name == 'chocolate chip')
rp = connection.execute(s)
r = rp.first()
print(r)
for key in r.keys():
print('{:>20}:{}'.format(key, r[key]))

@ -0,0 +1,6 @@
from sqlalchemy import delete, and_
from s1_models import *
d = delete(cookies).where(and_(cookies.c.cookie_name.like('%raisin%'), cookies.c.quantity > 500))
result = connection.execute(d)
print(result.rowcount)

@ -0,0 +1,21 @@
from sqlalchemy import select, func
from s1_models import *
# join
columns = [orders.c.order_id, users.c.username, users.c.phone, cookies.c.cookie_name, line_items.c.quantity,
line_items.c.extended_cost]
cookiemon_orders = select(columns)
cookiemon_orders = cookiemon_orders.select_from(orders.join(users).join(line_items).join(cookies)).where(
users.c.username == 'cookiemon')
result = connection.execute(cookiemon_orders).fetchall()
for row in result:
print(row)
# outerjoin
columns = [users.c.username, func.count(orders.c.order_id)]
all_orders = select(columns)
all_orders = all_orders.select_from(users.outerjoin(orders))
all_orders = all_orders.group_by(users.c.username)
result = connection.execute(all_orders).fetchall()
for row in result:
print(row)
Loading…
Cancel
Save