From 33b245d70f9f0163755e6401624b980a32f59897 Mon Sep 17 00:00:00 2001 From: roger Date: Mon, 19 Jun 2023 18:10:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0crud=E7=9A=84=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlalchemy_1x/data_prepare.py | 64 +++++++++++++++++ main.py => sqlalchemy_1x/main.py | 0 s1_models.py => sqlalchemy_1x/s1_models.py | 4 +- .../s2.1_insert_data.py | 0 .../s2.2_fetch_data.py | 0 sqlalchemy_1x/s2.3_filter_data.py | 69 +++++++++++++++++++ sqlalchemy_1x/s2.4_update_data.py | 14 ++++ sqlalchemy_1x/s2.5_delete_data.py | 6 ++ sqlalchemy_1x/s2.6_join.py | 21 ++++++ 9 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 sqlalchemy_1x/data_prepare.py rename main.py => sqlalchemy_1x/main.py (100%) rename s1_models.py => sqlalchemy_1x/s1_models.py (97%) rename s2_insert_data.py => sqlalchemy_1x/s2.1_insert_data.py (100%) rename s2_fetch_data.py => sqlalchemy_1x/s2.2_fetch_data.py (100%) create mode 100644 sqlalchemy_1x/s2.3_filter_data.py create mode 100644 sqlalchemy_1x/s2.4_update_data.py create mode 100644 sqlalchemy_1x/s2.5_delete_data.py create mode 100644 sqlalchemy_1x/s2.6_join.py diff --git a/sqlalchemy_1x/data_prepare.py b/sqlalchemy_1x/data_prepare.py new file mode 100644 index 0000000..79fd886 --- /dev/null +++ b/sqlalchemy_1x/data_prepare.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) diff --git a/main.py b/sqlalchemy_1x/main.py similarity index 100% rename from main.py rename to sqlalchemy_1x/main.py diff --git a/s1_models.py b/sqlalchemy_1x/s1_models.py similarity index 97% rename from s1_models.py rename to sqlalchemy_1x/s1_models.py index 9eefb93..39dec9f 100644 --- a/s1_models.py +++ b/sqlalchemy_1x/s1_models.py @@ -25,7 +25,7 @@ orders = Table('orders', metadata, Column('order_id', Integer(), primary_key=True), Column('user_id', ForeignKey('users.user_id')), Column('shipped', Boolean(), default=False) -) + ) line_items = Table('line_items', metadata, Column('line_items_id', Integer(), primary_key=True), Column('order_id', ForeignKey('orders.order_id')), @@ -38,4 +38,4 @@ DB_URI = "mysql+pymysql://root:Sxzgx1209@home.rogersun.cn:3306/orm_sqlalchemy" engine = create_engine(DB_URI, echo=True) metadata.create_all(engine) -connection = engine.connect() \ No newline at end of file +connection = engine.connect() diff --git a/s2_insert_data.py b/sqlalchemy_1x/s2.1_insert_data.py similarity index 100% rename from s2_insert_data.py rename to sqlalchemy_1x/s2.1_insert_data.py diff --git a/s2_fetch_data.py b/sqlalchemy_1x/s2.2_fetch_data.py similarity index 100% rename from s2_fetch_data.py rename to sqlalchemy_1x/s2.2_fetch_data.py diff --git a/sqlalchemy_1x/s2.3_filter_data.py b/sqlalchemy_1x/s2.3_filter_data.py new file mode 100644 index 0000000..4af53a4 --- /dev/null +++ b/sqlalchemy_1x/s2.3_filter_data.py @@ -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) diff --git a/sqlalchemy_1x/s2.4_update_data.py b/sqlalchemy_1x/s2.4_update_data.py new file mode 100644 index 0000000..dad82a1 --- /dev/null +++ b/sqlalchemy_1x/s2.4_update_data.py @@ -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])) diff --git a/sqlalchemy_1x/s2.5_delete_data.py b/sqlalchemy_1x/s2.5_delete_data.py new file mode 100644 index 0000000..f238222 --- /dev/null +++ b/sqlalchemy_1x/s2.5_delete_data.py @@ -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) diff --git a/sqlalchemy_1x/s2.6_join.py b/sqlalchemy_1x/s2.6_join.py new file mode 100644 index 0000000..3a52d4b --- /dev/null +++ b/sqlalchemy_1x/s2.6_join.py @@ -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)