From 22414e979e1b856d3013fa3db7cba35c38d0096f Mon Sep 17 00:00:00 2001 From: roger_home_pc Date: Sun, 18 Jun 2023 20:23:48 +0800 Subject: [PATCH] first commit --- main.py | 16 +++++++++++++ s1_models.py | 41 +++++++++++++++++++++++++++++++ s2_fetch_data.py | 22 +++++++++++++++++ s2_insert_data.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 main.py create mode 100644 s1_models.py create mode 100644 s2_fetch_data.py create mode 100644 s2_insert_data.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..5596b44 --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +# This is a sample Python script. + +# Press Shift+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + + +def print_hi(name): + # Use a breakpoint in the code line below to debug your script. + print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + print_hi('PyCharm') + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git a/s1_models.py b/s1_models.py new file mode 100644 index 0000000..9eefb93 --- /dev/null +++ b/s1_models.py @@ -0,0 +1,41 @@ +from datetime import datetime +from sqlalchemy import (MetaData, Table, Column, Integer, Numeric, String, Boolean, DateTime, ForeignKey, create_engine) + +metadata = MetaData() + +cookies = Table('cookies', metadata, + Column('cookie_id', Integer(), primary_key=True), + Column('cookie_name', String(50), index=True), + Column('cookie_recipe_url', String(255)), + Column('cookie_sku', String(55)), + Column('quantity', Integer()), + Column('unit_cost', Numeric(12, 2)) + ) +users = Table('users', metadata, + Column('user_id', Integer(), primary_key=True), + Column('customer_number', Integer(), autoincrement=True), + Column('username', String(15), nullable=False, unique=True), + Column('email_address', String(255), nullable=False), + Column('phone', String(20), nullable=False), + Column('password', String(25), nullable=False), + Column('created_on', DateTime(), default=datetime.now), + Column('updated_on', DateTime(), default=datetime.now, onupdate=datetime.now) + ) +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')), + Column('cookie_id', ForeignKey('cookies.cookie_id')), + Column('quantity', Integer()), + Column('extended_cost', Numeric(12, 2)) + ) + +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 diff --git a/s2_fetch_data.py b/s2_fetch_data.py new file mode 100644 index 0000000..41b77f5 --- /dev/null +++ b/s2_fetch_data.py @@ -0,0 +1,22 @@ +from sqlalchemy import select +from s1_models import * + +# 方法1 +s1 = select([cookies]) # 2.0废弃 +rp1 = connection.execute(s1) +results1 = rp1.fetchall() +print(results1) + +# 方法2 +s2 = cookies.select() +str(s2) +rp2 = connection.execute(s2) +results2 = rp2.fetchall() +print(results2) +print(results2[0]) +first_row = results2[0] +print(first_row[1]) +print(first_row.cookie_name) +print(first_row[cookies.c.cookie_name]) +results3 = rp2.scalar() +print(results3) \ No newline at end of file diff --git a/s2_insert_data.py b/s2_insert_data.py new file mode 100644 index 0000000..05d6180 --- /dev/null +++ b/s2_insert_data.py @@ -0,0 +1,61 @@ +from s1_models import * +from sqlalchemy import insert + +# 方式1 +ins1 = cookies.insert().values( + cookie_name="chocolate chip", + cookie_recipe_url="http://some.aweso.me/cookie/recipe.html", + cookie_sku="CC01", + quantity="12", + unit_cost="0.50" +) + +# 方式2 +ins2 = insert(cookies).values( + cookie_name="chocolate chip", + cookie_recipe_url="http://some.aweso.me/cookie/recipe.html", + cookie_sku="CC01", + quantity="12", + unit_cost="0.50" +) +# 打印 INSERT 语句的结构,不包含数据 +print(str(ins1)) +# 打印插入的数据 +print(ins1.compile().params) +# 执行插入 +# result1 = connection.execute(ins1) + +# print(result1.inserted_primary_key) + +# 方式3 +ins3 = cookies.insert() +result3 = connection.execute( + ins3, + cookie_name="dark chocolate chip1", + cookie_recipe_url="http://some.aweso.me/cookie/recipe_dark.html", + cookie_sku="CC02", + quantity="1", + unit_cost="0.75" +) +# print(result3.inserted_primary_key) + +ins4 = cookies.insert() +inventory_list = [ + { + 'cookie_name': 'peanut butter', + 'cookie_recipe_url': 'http://some.aweso.me/cookie/peanut.html', + 'cookie_sku': 'PB01', + 'quantity': '24', + 'unit_cost': '0.25' + }, + { + 'cookie_name': 'oatmeal raisin', + 'cookie_recipe_url': 'http://some.okay.me/cookie/raisin.html', + 'cookie_sku': 'EWW01', + 'quantity': '100', + 'unit_cost': '1.00' + } +] + +result4 = connection.execute(ins4, inventory_list) +print(result4.inserted_primary_key_rows)