From 5c46fc02680b999ca2e0557ed71a35daa141a28f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=86=E5=85=89=E5=B9=B4?= <1738803808@qq.com> Date: Mon, 29 Mar 2021 23:29:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20week8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../week8/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/.keep" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/.keep" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/.keep" new file mode 100644 index 00000000..e69de29b -- Gitee From bfbfc629869d381901082020d9f169036297545f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=86=E5=85=89=E5=B9=B4?= <1738803808@qq.com> Date: Mon, 29 Mar 2021 23:30:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?3=E7=8F=AD=5F=E9=80=86=E5=85=89=E5=B9=B4=5F?= =?UTF-8?q?=E7=AC=AC=E5=85=AB=E5=91=A8=E7=AC=AC=E4=B8=80=E3=80=81=E4=BA=8C?= =?UTF-8?q?=E3=80=81=E4=B8=89=E8=8A=82=E4=BD=9C=E4=B8=9A=203=E7=8F=AD=5F?= =?UTF-8?q?=E9=80=86=E5=85=89=E5=B9=B4=5F=E7=AC=AC=E5=85=AB=E5=91=A8?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E3=80=81=E4=BA=8C=E3=80=81=E4=B8=89=E8=8A=82?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../week8/lesson_03.py" | 103 ++++++++++++++++++ ...76\345\220\216\344\275\234\344\270\232.md" | 17 +++ ...76\345\220\216\344\275\234\344\270\232.md" | 18 +++ 3 files changed, 138 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/lesson_03.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/\347\254\254\345\205\253\345\221\250-\347\254\254\344\272\214\350\212\202-\350\257\276\345\220\216\344\275\234\344\270\232.md" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/\347\254\254\345\205\253\345\221\250_\347\254\254\344\270\200\350\212\202_\350\257\276\345\220\216\344\275\234\344\270\232.md" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/lesson_03.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/lesson_03.py" new file mode 100644 index 00000000..eb69efc2 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/lesson_03.py" @@ -0,0 +1,103 @@ +import pymysql +import random +import time + +from week_3.lesson_03 import clock_it_deco + +MYSQL_CONF = { + "host": "127.0.0.1", + "user": "root", + "password": "admin", + "db": "my_class" +} + +# 连接数据库 +mysql_con = pymysql.connect(**MYSQL_CONF) + +# 简单理解真正执行语句的线程 +mysql_cursor = mysql_con.cursor() + +# 单挑执行 +@clock_it_deco +def insert_one(): + for i in range(10**4): + store_name = f"店铺_{i}" + amount = format(random.uniform(10**3, 10**6), '.2f') + department = f"事业部_{random.randint(1, 10)}" + sta_date = time.strftime("%Y-%m-%d") + + SQL = f"""INSERT INTO store_perf(store_name, amount, department, sta_date) + VALUES ('{store_name}', {amount}, '{department}', '{sta_date}')""" + print(SQL) + mysql_cursor.execute(SQL) + + # 显示执行commit, 避免cursor执行, 但是数据库未收到的执行指令的情况 + mysql_con.commit() + +# 多条执行 +@clock_it_deco +def insert_one(): + for i in range(10**4): + store_name = f"店铺_{i}" + amount = format(random.uniform(10**3, 10**6), '.2f') + department = f"事业部_{random.randint(1, 10)}" + sta_date = time.strftime("%Y-%m-%d") + + SQL = f"""INSERT INTO store_perf(store_name, amount, department, sta_date) + VALUES ('{store_name}', {amount}, '{department}', '{sta_date}')""" + print(SQL) + mysql_cursor.execute(SQL) + + # 显示执行commit, 避免cursor执行, 但是数据库未收到的执行指令的情况 + mysql_con.commit() + +@clock_it_deco +def insert_many(): + values = [] + for i in range(10**4): + store_name = f"店铺_{i}" + amount = format(random.uniform(10**3, 10**6), '.2f') + department = f"事业部_{random.randint(1, 10)}" + sta_date = time.strftime("%Y-%m-%d") + values.append((store_name, amount, department, sta_date)) + + SQL = """INSERT INTO store_perf(store_name, amount, department, sta_date) + VALUES (%s, %s, %s, %s)""" + print(values) + mysql_cursor.executemany(SQL, values) + mysql_con.commit() + + +def get_shops(): + SQL = "select store_name, amount, sta_date from store_perf where department='事业部_1' LIMIT 2" + mysql_cursor.execute(SQL) + # 获取返回值 + query_set = mysql_cursor.fetchall() + print(query_set) + + +def transaction(): + try: + SQL = "DELETE FROM store_perf where store_name='店铺_13'" + mysql_cursor.execute(SQL) + # SQL_2 = "INSERT INTO store_perf VALUES(name)" + # mysql_cursor.execute(SQL_2) + + except Exception as e: + print("raise Exceptions", e.args[0]) + print("rollback") + mysql_con.rollback() + finally: + mysql_con.commit() + +if __name__ == "__main__": + # insert_one() + ''' + INSERT INTO store_perf(store_name, amount, department, sta_date) + VALUES ('店铺_9999', 933657.09, '事业部_1', '2021-03-28') + insert_one execute time: 19.46 s + ''' + # insert_many() + # insert_many execute time: 0.68 s + # get_shops() + transaction() \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/\347\254\254\345\205\253\345\221\250-\347\254\254\344\272\214\350\212\202-\350\257\276\345\220\216\344\275\234\344\270\232.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/\347\254\254\345\205\253\345\221\250-\347\254\254\344\272\214\350\212\202-\350\257\276\345\220\216\344\275\234\344\270\232.md" new file mode 100644 index 00000000..4ae4273b --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/\347\254\254\345\205\253\345\221\250-\347\254\254\344\272\214\350\212\202-\350\257\276\345\220\216\344\275\234\344\270\232.md" @@ -0,0 +1,17 @@ +## 第八周-第二节-课后作业 + +- 重点掌握用户变量, 练习变量的赋值和使用 + + ![image-20210324225237399](C:\Users\Lin\AppData\Roaming\Typora\typora-user-images\image-20210324225237399.png) + + ​ ![image-20210324225653055](C:\Users\Lin\AppData\Roaming\Typora\typora-user-images\image-20210324225653055.png) + +- 练习存储过程(能自行完整地写出一个函数) + + ![image-20210324224532493](C:\Users\Lin\AppData\Roaming\Typora\typora-user-images\image-20210324224532493.png) + + + +- 练习触发器(只要求操作几次) + + ![image-20210324230153289](C:\Users\Lin\AppData\Roaming\Typora\typora-user-images\image-20210324230153289.png) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/\347\254\254\345\205\253\345\221\250_\347\254\254\344\270\200\350\212\202_\350\257\276\345\220\216\344\275\234\344\270\232.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/\347\254\254\345\205\253\345\221\250_\347\254\254\344\270\200\350\212\202_\350\257\276\345\220\216\344\275\234\344\270\232.md" new file mode 100644 index 00000000..133ace5a --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\200\206\345\205\211\345\271\264/week8/\347\254\254\345\205\253\345\221\250_\347\254\254\344\270\200\350\212\202_\350\257\276\345\220\216\344\275\234\344\270\232.md" @@ -0,0 +1,18 @@ +## 第八周-第一节-课后作业 + +- 简单使用Mysql约束, 并举例说明使用场景. + + > UNIQUE、primary key:用于学生信息表中的学号或者身份证号 + > + > not null:用于必填信息不能不填 + + ![image-20210322125130212](C:\Users\Lin\AppData\Roaming\Typora\typora-user-images\image-20210322125130212.png) + +![image-20210324205132889](C:\Users\Lin\AppData\Roaming\Typora\typora-user-images\image-20210324205132889.png) + +- 练习使用分组查询.(group by) + + ![image-20210324205331283](C:\Users\Lin\AppData\Roaming\Typora\typora-user-images\image-20210324205331283.png) + + + -- Gitee