zxy0307 commited on
Commit
cf95459
·
verified ·
1 Parent(s): 3fd414d

Upload 3 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ 03_Redis数据库概述.ipynb filter=lfs diff=lfs merge=lfs -text
01_Python操作MySQL.ipynb ADDED
@@ -0,0 +1,592 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "84513081",
6
+ "metadata": {},
7
+ "source": [
8
+ "## 查看数据"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 1,
14
+ "id": "23241985",
15
+ "metadata": {
16
+ "ExecuteTime": {
17
+ "end_time": "2023-10-10T12:10:53.493078Z",
18
+ "start_time": "2023-10-10T12:10:53.417598Z"
19
+ },
20
+ "scrolled": true
21
+ },
22
+ "outputs": [
23
+ {
24
+ "name": "stdout",
25
+ "output_type": "stream",
26
+ "text": [
27
+ "<pymysql.connections.Connection object at 0x106d22760>\n",
28
+ "(5, '孙铭阳', '男', 20, '广东', '23446523', '23446523@qq.com', 'zhangsan', '123')\n",
29
+ "(17, '李四', '女', 26, '广东', '12345123', 'lisi@qq.com', None, None)\n",
30
+ "(21, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
31
+ "(22, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
32
+ "(25, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
33
+ "(27, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
34
+ "(28, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
35
+ "(29, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
36
+ "(30, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
37
+ "(31, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
38
+ "(32, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
39
+ "(33, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
40
+ "(34, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
41
+ "(35, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
42
+ "(36, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
43
+ "(37, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
44
+ "(38, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
45
+ "(39, '李永波', '女', 43, '广东', '236543', '236543@qq.com', None, None)\n",
46
+ "(40, '00', '男', 78, '广东', '99', '123', None, None)\n"
47
+ ]
48
+ }
49
+ ],
50
+ "source": [
51
+ "import pymysql\n",
52
+ "\n",
53
+ "## 连接数据库\n",
54
+ "# 获取连接\n",
55
+ "connection = pymysql.connect(\n",
56
+ " host=\"localhost\", user=\"root\", password=\"zxydsg123\", \n",
57
+ " db=\"User_information_case\", # 数据库名称\n",
58
+ " charset='utf8') # 编码 UTF-8\n",
59
+ "\n",
60
+ "print(connection)\n",
61
+ "\n",
62
+ "# 创建 cursor对象\n",
63
+ "cursor = connection.cursor()\n",
64
+ "\n",
65
+ "# 编写 sql\n",
66
+ "sql = \"select * from user\"\n",
67
+ "\n",
68
+ "# 执行 SQL\n",
69
+ "cursor.execute(sql)\n",
70
+ "\n",
71
+ "# 获取结果集\n",
72
+ "users = cursor.fetchall()\n",
73
+ "\n",
74
+ "# 查看结果\n",
75
+ "for u in users:\n",
76
+ " print(u)"
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "markdown",
81
+ "id": "ebb6eacc",
82
+ "metadata": {},
83
+ "source": [
84
+ "## 插入数据"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "code",
89
+ "execution_count": 2,
90
+ "id": "eb638782",
91
+ "metadata": {
92
+ "ExecuteTime": {
93
+ "end_time": "2023-10-10T12:15:52.983647Z",
94
+ "start_time": "2023-10-10T12:15:52.960757Z"
95
+ }
96
+ },
97
+ "outputs": [
98
+ {
99
+ "name": "stdout",
100
+ "output_type": "stream",
101
+ "text": [
102
+ "count: 1\n"
103
+ ]
104
+ }
105
+ ],
106
+ "source": [
107
+ "import pymysql\n",
108
+ "\n",
109
+ "# 获取连接\n",
110
+ "connection = pymysql.connect(\n",
111
+ " host=\"localhost\", user=\"root\", password=\"zxydsg123\",\n",
112
+ " db=\"User_information_case\", # 数据库名称\n",
113
+ " charset=\"utf8\") # 编码 UTF-8\n",
114
+ "\n",
115
+ "# 获取cursor对象\n",
116
+ "cursor = connection.cursor()\n",
117
+ "\n",
118
+ "# 编写sql\n",
119
+ "sql = \"insert into user(id, name) values(41, 'Tom')\"\n",
120
+ "\n",
121
+ "# 执行sql,返回 影响的行数\n",
122
+ "count = cursor.execute(sql)\n",
123
+ "\n",
124
+ "print(\"count: \", count)\n",
125
+ "\n",
126
+ "# 执行DML时候,会开启一个事务,需要我们主动提交 -- 防止对数据进行误操作\n",
127
+ "connection.commit()"
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "markdown",
132
+ "id": "4e53b475",
133
+ "metadata": {},
134
+ "source": [
135
+ "## 修改数据"
136
+ ]
137
+ },
138
+ {
139
+ "cell_type": "code",
140
+ "execution_count": 4,
141
+ "id": "af994da0",
142
+ "metadata": {
143
+ "ExecuteTime": {
144
+ "end_time": "2023-10-10T12:18:28.072611Z",
145
+ "start_time": "2023-10-10T12:18:28.062564Z"
146
+ }
147
+ },
148
+ "outputs": [
149
+ {
150
+ "name": "stdout",
151
+ "output_type": "stream",
152
+ "text": [
153
+ "count: 1\n"
154
+ ]
155
+ }
156
+ ],
157
+ "source": [
158
+ "import pymysql\n",
159
+ "\n",
160
+ "# 获取连接\n",
161
+ "connection = pymysql.connect(\n",
162
+ " host=\"localhost\", user=\"root\", password=\"zxydsg123\", \n",
163
+ " db=\"User_information_case\", # 数据库名称\n",
164
+ " charset=\"utf8\") # 编码 UTF-8\n",
165
+ "\n",
166
+ "# 获取cursor对象\n",
167
+ "cursor = connection.cursor()\n",
168
+ "\n",
169
+ "# 编写sql\n",
170
+ "sql = \"update user set gender='男' where id=41\"\n",
171
+ "\n",
172
+ "# 执行sql,返回受影响的行数\n",
173
+ "count = cursor.execute(sql)\n",
174
+ "print(\"count:\", count)\n",
175
+ "\n",
176
+ "# 执行DML时候,会开启一个事务,需要我们主动提交 -- 防止对数据进行误操作\n",
177
+ "connection.commit()"
178
+ ]
179
+ },
180
+ {
181
+ "cell_type": "markdown",
182
+ "id": "7155a879",
183
+ "metadata": {},
184
+ "source": [
185
+ "## 删除数据"
186
+ ]
187
+ },
188
+ {
189
+ "cell_type": "code",
190
+ "execution_count": 5,
191
+ "id": "985d4f0b",
192
+ "metadata": {
193
+ "ExecuteTime": {
194
+ "end_time": "2023-10-10T12:20:45.873650Z",
195
+ "start_time": "2023-10-10T12:20:45.864234Z"
196
+ }
197
+ },
198
+ "outputs": [
199
+ {
200
+ "name": "stdout",
201
+ "output_type": "stream",
202
+ "text": [
203
+ "count: 1\n"
204
+ ]
205
+ }
206
+ ],
207
+ "source": [
208
+ "import pymysql\n",
209
+ "\n",
210
+ "# 获取连接\n",
211
+ "connection = pymysql.connect(\n",
212
+ " host=\"localhost\", user=\"root\", password=\"zxydsg123\", \n",
213
+ " db=\"User_information_case\", # 数据库名称\n",
214
+ " charset=\"utf8\") # 编码 UTF-8\n",
215
+ "\n",
216
+ "# 获取cursor对象\n",
217
+ "cursor = connection.cursor()\n",
218
+ "\n",
219
+ "# 编写sql\n",
220
+ "sql = \"delete from user where id=41\"\n",
221
+ "\n",
222
+ "# 执行sql,返回受影响的行数\n",
223
+ "count = cursor.execute(sql)\n",
224
+ "print(\"count:\", count)\n",
225
+ "\n",
226
+ "# 执行DML时候,会开启一个事务,需要我们主动提交 -- 防止对数据进行误操作\n",
227
+ "connection.commit()"
228
+ ]
229
+ },
230
+ {
231
+ "cell_type": "markdown",
232
+ "id": "56bce4d3",
233
+ "metadata": {},
234
+ "source": [
235
+ "## 查询漏洞"
236
+ ]
237
+ },
238
+ {
239
+ "cell_type": "code",
240
+ "execution_count": 7,
241
+ "id": "29422cfe",
242
+ "metadata": {
243
+ "ExecuteTime": {
244
+ "end_time": "2023-10-10T12:24:40.726658Z",
245
+ "start_time": "2023-10-10T12:24:40.717935Z"
246
+ }
247
+ },
248
+ "outputs": [
249
+ {
250
+ "name": "stdout",
251
+ "output_type": "stream",
252
+ "text": [
253
+ "count: 19\n"
254
+ ]
255
+ }
256
+ ],
257
+ "source": [
258
+ "import pymysql\n",
259
+ "# 获取连接\n",
260
+ "connection = pymysql.connect(\n",
261
+ " host=\"localhost\", user=\"root\", password=\"zxydsg123\", \n",
262
+ " db=\"User_information_case\", # 数据库名称\n",
263
+ " charset=\"utf8\") # 编码 UTF-8\n",
264
+ "\n",
265
+ "# 获取cursor对象\n",
266
+ "cursor = connection.cursor()\n",
267
+ "\n",
268
+ "# 编写sql\n",
269
+ "sql = \"select * from user where name=%s\" % (\"'scott' or 1=1\")\n",
270
+ "\n",
271
+ "# 执行sql\n",
272
+ "count = cursor.execute(sql)\n",
273
+ "print(\"count:\", count)\n",
274
+ "\n",
275
+ "# 执行DML时候,会开启一个事务,需要我们主动提交 -- 防止对数据进行误操作\n",
276
+ "connection.commit()"
277
+ ]
278
+ },
279
+ {
280
+ "cell_type": "markdown",
281
+ "id": "706a683b",
282
+ "metadata": {},
283
+ "source": [
284
+ "## 传递参数"
285
+ ]
286
+ },
287
+ {
288
+ "cell_type": "code",
289
+ "execution_count": 8,
290
+ "id": "a468b121",
291
+ "metadata": {
292
+ "ExecuteTime": {
293
+ "end_time": "2023-10-10T12:26:10.322260Z",
294
+ "start_time": "2023-10-10T12:26:10.310338Z"
295
+ }
296
+ },
297
+ "outputs": [
298
+ {
299
+ "name": "stdout",
300
+ "output_type": "stream",
301
+ "text": [
302
+ "count: 1\n"
303
+ ]
304
+ }
305
+ ],
306
+ "source": [
307
+ "import pymysql\n",
308
+ "# 获取连接\n",
309
+ "connection = pymysql.connect(\n",
310
+ " host=\"localhost\", user=\"root\", password=\"zxydsg123\", \n",
311
+ " db=\"User_information_case\", # 数据库名称\n",
312
+ " charset=\"utf8\") # 编码 UTF-8\n",
313
+ "\n",
314
+ "# 获取cursor对象\n",
315
+ "cursor = connection.cursor()\n",
316
+ "\n",
317
+ "# 编写sql\n",
318
+ "sql = \"insert into user(id, name) values(%s, %s)\"\n",
319
+ "\n",
320
+ "# 执行sql\n",
321
+ "args = (41, 'marry')\n",
322
+ "count = cursor.execute(sql, args)\n",
323
+ "print(\"count:\", count)\n",
324
+ "\n",
325
+ "# 执行DML时候,会开启一个事务,需要我们主动提交 -- 防止对数据进行误操作\n",
326
+ "connection.commit()"
327
+ ]
328
+ },
329
+ {
330
+ "cell_type": "markdown",
331
+ "id": "84a00649",
332
+ "metadata": {},
333
+ "source": [
334
+ "## 添加异常处理"
335
+ ]
336
+ },
337
+ {
338
+ "cell_type": "code",
339
+ "execution_count": 9,
340
+ "id": "d24cf6a9",
341
+ "metadata": {
342
+ "ExecuteTime": {
343
+ "end_time": "2023-10-10T12:32:36.642368Z",
344
+ "start_time": "2023-10-10T12:32:36.630347Z"
345
+ },
346
+ "scrolled": true
347
+ },
348
+ "outputs": [
349
+ {
350
+ "name": "stdout",
351
+ "output_type": "stream",
352
+ "text": [
353
+ "count: 1\n"
354
+ ]
355
+ }
356
+ ],
357
+ "source": [
358
+ "import pymysql\n",
359
+ "connection = None\n",
360
+ "cursor = None\n",
361
+ "\n",
362
+ "try:\n",
363
+ " # 获取连接\n",
364
+ " connection = pymysql.connect(\n",
365
+ " host=\"localhost\", user=\"root\", password=\"zxydsg123\", \n",
366
+ " db=\"User_information_case\", # 数据库名称\n",
367
+ " charset=\"utf8\") # 编码 UTF-8\n",
368
+ " \n",
369
+ " # 获取 cursor对象\n",
370
+ " cursor = connection.cursor()\n",
371
+ " \n",
372
+ " # 编写sql\n",
373
+ " sql = \"insert into user(id, name) values(%s, %s)\"\n",
374
+ " \n",
375
+ " # 执行sql\n",
376
+ " args = (1234, 'Tom')\n",
377
+ " count = cursor.execute(sql, args)\n",
378
+ " print(\"count:\", count)\n",
379
+ " \n",
380
+ " # 执行DML时候,会开启一个事务,需要我们主动提交\n",
381
+ " connection.commit()\n",
382
+ " \n",
383
+ "except Exception as e:\n",
384
+ " # 如果捕获到异常\n",
385
+ " print(e)\n",
386
+ " \n",
387
+ " if connection:\n",
388
+ " # 回滚事务\n",
389
+ " connection.rollback()\n",
390
+ " \n",
391
+ "finally:\n",
392
+ " # 无论是否捕获到异常,我们都要关闭cursor,释放资源\n",
393
+ " if cursor:\n",
394
+ " cursor.close()\n",
395
+ " \n",
396
+ " if connection:\n",
397
+ " cursor.close()"
398
+ ]
399
+ },
400
+ {
401
+ "cell_type": "markdown",
402
+ "id": "874055be",
403
+ "metadata": {},
404
+ "source": [
405
+ "## DBUtil功能封装"
406
+ ]
407
+ },
408
+ {
409
+ "cell_type": "code",
410
+ "execution_count": 11,
411
+ "id": "8873d37e",
412
+ "metadata": {
413
+ "ExecuteTime": {
414
+ "end_time": "2023-10-10T12:44:33.199935Z",
415
+ "start_time": "2023-10-10T12:44:33.175199Z"
416
+ }
417
+ },
418
+ "outputs": [
419
+ {
420
+ "name": "stdout",
421
+ "output_type": "stream",
422
+ "text": [
423
+ "(1062, \"Duplicate entry '9999' for key 'user.PRIMARY'\")\n",
424
+ "(9999, 'lili', '女', None, None, None, None, None, None)\n",
425
+ "(5, '孙铭阳', '男', 20, '广东', '23446523', '23446523@qq.com', 'zhangsan', '123')\n",
426
+ "(17, '李四', '女', 26, '广东', '12345123', 'lisi@qq.com', None, None)\n",
427
+ "(21, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
428
+ "(22, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
429
+ "(25, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
430
+ "(27, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
431
+ "(28, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
432
+ "(29, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
433
+ "(30, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
434
+ "(31, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
435
+ "(32, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
436
+ "(33, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
437
+ "(34, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
438
+ "(35, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
439
+ "(36, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
440
+ "(37, '李四', '男', 24, '广东', '12345123', 'lisi@qq.com', None, None)\n",
441
+ "(38, '王五', '男', 25, '湖南', '12345123', 'lisi@qq.com', None, None)\n",
442
+ "(39, '李永波', '女', 43, '广东', '236543', '236543@qq.com', None, None)\n",
443
+ "(40, '00', '男', 78, '广东', '99', '123', None, None)\n",
444
+ "(41, 'marry', None, None, None, None, None, None, None)\n",
445
+ "(1234, 'Tom', None, None, None, None, None, None, None)\n",
446
+ "(9999, 'lili', '女', None, None, None, None, None, None)\n"
447
+ ]
448
+ }
449
+ ],
450
+ "source": [
451
+ "import pymysql\n",
452
+ "\n",
453
+ "\n",
454
+ "class DBUtil:\n",
455
+ " \"\"\"数据库工具类\"\"\"\n",
456
+ "\n",
457
+ " config = {\n",
458
+ " \"host\": \"localhost\",\n",
459
+ " \"user\": \"root\",\n",
460
+ " \"password\": \"zxydsg123\",\n",
461
+ " \"db\": \"User_information_case\", # 数据库名称\n",
462
+ " \"charset\": \"utf8\" # 编码 UTF-8\n",
463
+ " }\n",
464
+ "\n",
465
+ " def __init__(self):\n",
466
+ "\n",
467
+ " # 建立连接,传入参数\n",
468
+ " self.connection = pymysql.connect(**DBUtil.config)\n",
469
+ "\n",
470
+ " # 创建 cursor对象\n",
471
+ " self.cursor = self.connection.cursor()\n",
472
+ "\n",
473
+ " def close(self):\n",
474
+ " \"\"\"释放资源\"\"\"\n",
475
+ " if self.cursor:\n",
476
+ " self.cursor.close()\n",
477
+ " if self.connection:\n",
478
+ " self.connection.close()\n",
479
+ "\n",
480
+ " def exeDML(self, sql, *args):\n",
481
+ " \"\"\"DML(Data Manipulation Language)语句: 数据操纵语言 -- 插入 修改 删除调用\"\"\"\n",
482
+ " try:\n",
483
+ " # 执行sql,返回影响到的行数\n",
484
+ " count = self.cursor.execute(sql, args)\n",
485
+ " \n",
486
+ " # 提交事务\n",
487
+ " self.connection.commit()\n",
488
+ " \n",
489
+ " return count\n",
490
+ " \n",
491
+ " # 如果捕获到异常\n",
492
+ " except Exception as e:\n",
493
+ " print(e)\n",
494
+ " \n",
495
+ " # 回滚食物\n",
496
+ " if self.connection:\n",
497
+ " self.connection.rollback()\n",
498
+ "\n",
499
+ " def query_one(self, sql, *args):\n",
500
+ " \"\"\"查询单行数据\"\"\"\n",
501
+ " try:\n",
502
+ " # 执行sql\n",
503
+ " self.cursor.execute(sql, args)\n",
504
+ " \n",
505
+ " # 获取结果集\n",
506
+ " return self.cursor.fetchone()\n",
507
+ " \n",
508
+ " except Exception as e:\n",
509
+ " print(e)\n",
510
+ " \n",
511
+ " def query_all(self, sql, *args):\n",
512
+ " \"\"\"查询所有数据\"\"\"\n",
513
+ " try:\n",
514
+ " # 执行sql\n",
515
+ " self.cursor.execute(sql, args)\n",
516
+ " # 获取结果集\n",
517
+ " return self.cursor.fetchall()\n",
518
+ " \n",
519
+ " except Exception as e:\n",
520
+ " print(e)\n",
521
+ "\n",
522
+ "\n",
523
+ "if __name__ == \"__main__\":\n",
524
+ " \n",
525
+ " # 创建 DBUtil\n",
526
+ " dbutil = DBUtil()\n",
527
+ " \n",
528
+ " # 插入数据\n",
529
+ " sql = \"insert into user(id, name, gender) values(%s, %s, %s)\"\n",
530
+ " \n",
531
+ " count = dbutil.exeDML(sql, 9999,'lili', '女')\n",
532
+ "\n",
533
+ " \n",
534
+ " # 查询单行数据\n",
535
+ " sql = \"select * from user where id=%s\"\n",
536
+ "\n",
537
+ " # 打印查询结果\n",
538
+ " emp = dbutil.query_one(sql, 9999)\n",
539
+ " print(emp)\n",
540
+ "\n",
541
+ " \n",
542
+ " # 查询所有数据\n",
543
+ " sql = \"select * from user\"\n",
544
+ " users = dbutil.query_all(sql)\n",
545
+ " for u in users:\n",
546
+ " print(u)\n",
547
+ " \n",
548
+ " # 记得释放资源\n",
549
+ " dbutil.close()"
550
+ ]
551
+ }
552
+ ],
553
+ "metadata": {
554
+ "kernelspec": {
555
+ "display_name": "Python 3 (ipykernel)",
556
+ "language": "python",
557
+ "name": "python3"
558
+ },
559
+ "language_info": {
560
+ "codemirror_mode": {
561
+ "name": "ipython",
562
+ "version": 3
563
+ },
564
+ "file_extension": ".py",
565
+ "mimetype": "text/x-python",
566
+ "name": "python",
567
+ "nbconvert_exporter": "python",
568
+ "pygments_lexer": "ipython3",
569
+ "version": "3.9.12"
570
+ },
571
+ "toc": {
572
+ "base_numbering": 1,
573
+ "nav_menu": {},
574
+ "number_sections": true,
575
+ "sideBar": true,
576
+ "skip_h1_title": false,
577
+ "title_cell": "Table of Contents",
578
+ "title_sidebar": "Contents",
579
+ "toc_cell": false,
580
+ "toc_position": {
581
+ "height": "calc(100% - 180px)",
582
+ "left": "10px",
583
+ "top": "150px",
584
+ "width": "288px"
585
+ },
586
+ "toc_section_display": true,
587
+ "toc_window_display": true
588
+ }
589
+ },
590
+ "nbformat": 4,
591
+ "nbformat_minor": 5
592
+ }
02_项目:音乐播放器.ipynb ADDED
@@ -0,0 +1,383 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "ff211c10",
6
+ "metadata": {},
7
+ "source": [
8
+ "## 数据库工具类"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": null,
14
+ "id": "d43a7671",
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "import pymysql\n",
19
+ "\n",
20
+ "\n",
21
+ "class Mydb:\n",
22
+ " \"\"\"数据库工具类\"\"\"\n",
23
+ "\n",
24
+ " # 设置参数\n",
25
+ " config = {\n",
26
+ " \"host\": \"localhost\",\n",
27
+ " \"user\": \"root\",\n",
28
+ " \"password\": \"zxydsg123\",\n",
29
+ " \"db\": \"\",\n",
30
+ " \"charset\": \"utf8\"\n",
31
+ " }\n",
32
+ "\n",
33
+ " def __init__(self):\n",
34
+ " \"\"\"初始化\"\"\"\n",
35
+ " # 建立连接\n",
36
+ " self.connection = pymysql.connect(**Mydb.config)\n",
37
+ "\n",
38
+ " # 创建 cursor对象\n",
39
+ " self.cursor = self.connection.cursor()\n",
40
+ "\n",
41
+ " def close(self):\n",
42
+ " \"\"\"释放资源\"\"\"\n",
43
+ " if self.cursor:\n",
44
+ " self.cursor.close()\n",
45
+ " if self.connection:\n",
46
+ " self.connection.close()\n",
47
+ "\n",
48
+ " def exeDML(self, sql, *args):\n",
49
+ " \"\"\"插入 修改 删除调用\"\"\"\n",
50
+ " try:\n",
51
+ " # 执行sql\n",
52
+ " count = self.cursor.execute(sql, args)\n",
53
+ " id = self.connection.insert_id()\n",
54
+ " \n",
55
+ " # 提交事务\n",
56
+ " self.connection.commit()\n",
57
+ " return id\n",
58
+ " \n",
59
+ " # 如果捕获到异常,则回滚事务\n",
60
+ " except Exception as e:\n",
61
+ " print(e)\n",
62
+ " if self.connection:\n",
63
+ " self.connection.rollback()\n",
64
+ " \n",
65
+ "\n",
66
+ " def query_one(self, sql, *args):\n",
67
+ " \"\"\"查询单条数据\"\"\"\n",
68
+ " try:\n",
69
+ " # 执行sql\n",
70
+ " self.cursor.execute(sql, args)\n",
71
+ " \n",
72
+ " # 获取结果集\n",
73
+ " return self.cursor.fetchone()\n",
74
+ " \n",
75
+ " except Exception as e:\n",
76
+ " print(e)\n",
77
+ " \n",
78
+ "\n",
79
+ " def query_all(self, sql, *args):\n",
80
+ " \"\"\"查询全部数据\"\"\"\n",
81
+ " try:\n",
82
+ " # 执行sql\n",
83
+ " self.cursor.execute(sql, args)\n",
84
+ " \n",
85
+ " # 获取结果集\n",
86
+ " return self.cursor.fetchall()\n",
87
+ " \n",
88
+ " except Exception as e:\n",
89
+ " print(e)\n"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "markdown",
94
+ "id": "1f9ed17d",
95
+ "metadata": {},
96
+ "source": [
97
+ "## 提供服务类"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": null,
103
+ "id": "8326901a",
104
+ "metadata": {},
105
+ "outputs": [],
106
+ "source": [
107
+ "import pygame\n",
108
+ "\n",
109
+ "\n",
110
+ "class MyService:\n",
111
+ " def __init__(self):\n",
112
+ " self.user = None\n",
113
+ "\n",
114
+ " def login(self, uname, password):\n",
115
+ " \"\"\"登录\"\"\"\n",
116
+ "\n",
117
+ " # 获取 用户名、密码\n",
118
+ " sql = \"select * from t_user where uname=%s and password=%s\"\n",
119
+ " user = Mydb().query_one(sql, uname, password)\n",
120
+ "\n",
121
+ " # 比对信息\n",
122
+ " if user:\n",
123
+ " self.user = user\n",
124
+ " return True\n",
125
+ " else:\n",
126
+ " return False\n",
127
+ "\n",
128
+ " def add_music(self, files):\n",
129
+ " \"\"\"添加音乐\"\"\"\n",
130
+ "\n",
131
+ " # 遍历文件名\n",
132
+ " for f in files:\n",
133
+ "\n",
134
+ " # 前缀\n",
135
+ " start = f.rfind(\"/\")+1\n",
136
+ " \n",
137
+ " # 后缀\n",
138
+ " end = f.rfind(\".mp3\")\n",
139
+ " \n",
140
+ " # 音乐名\n",
141
+ " music_name = f[start:end]\n",
142
+ " \n",
143
+ " # 根据歌名查询数据库中,是否有该首歌\n",
144
+ " sql = \"select * from t_music where music_name=%s\"\n",
145
+ " music = Mydb().query_one(sql, music_name)\n",
146
+ " \n",
147
+ " # 如果数据库中存在 该音乐\n",
148
+ " if music:\n",
149
+ " \n",
150
+ " # 查询关联表t_list 该用户是否添加了这首歌\n",
151
+ " sql = \"select * from t_list where mid=%s and uid=%s\"\n",
152
+ " t_list = Mydb().query_one(sql, music[0], self.user[0])\n",
153
+ " \n",
154
+ " # 如果用户未添加该歌曲,添加\n",
155
+ " if not t_list:\n",
156
+ " sql = \"insert into t_list(mid,uid) values(%s,%s)\"\n",
157
+ " Mydb().exeDML(sql, music[0], self.user[0])\n",
158
+ " \n",
159
+ " # 数据库中如果没有该音乐\n",
160
+ " else:\n",
161
+ " # 将音乐保存到 数据库\n",
162
+ " sql = \"insert into t_music(music_name,path) values(%s,%s)\"\n",
163
+ " mid = Mydb().exeDML(sql, music_name, f)\n",
164
+ "\n",
165
+ " # 为用户添加音乐\n",
166
+ " sql = \"insert into t_list(mid,uid) values(%s,%s)\"\n",
167
+ " Mydb().exeDML(sql, mid, self.user[0])\n",
168
+ "\n",
169
+ " \n",
170
+ " def findListByUser(self):\n",
171
+ " \"\"\"查询用户的音乐列表\"\"\"\n",
172
+ " sql = \"select m.music_name from t_music m,t_list t where m.id=t.mid and t.uid=%s\"\n",
173
+ " return Mydb().query_all(sql, self.user[0])\n",
174
+ "\n",
175
+ "\n",
176
+ " def delete_music(self, music_name):\n",
177
+ " \"\"\"删除音乐\"\"\"\n",
178
+ " \n",
179
+ " # 根据音乐名称查询音乐id\n",
180
+ " sql = \"select id from t_music where music_name = %s\"\n",
181
+ " mid = Mydb().query_one(sql, music_name)\n",
182
+ " \n",
183
+ " # 为用户删除音乐,但数据库中不删除音乐\n",
184
+ " sql = \"delete from t_list where uid=%s and mid=%s\"\n",
185
+ " Mydb().exeDML(sql, self.user[0], mid[0])\n",
186
+ "\n",
187
+ " \n",
188
+ " def playMuisc(self, music_name):\n",
189
+ " \"\"\"播放音乐\"\"\"\n",
190
+ " \n",
191
+ " # 根据名称查询音乐的path\n",
192
+ " sql = \"select path from t_music where music_name=%s\"\n",
193
+ " path = Mydb().query_one(sql, music_name)\n",
194
+ " \n",
195
+ " # 初始化 mixer\n",
196
+ " pygame.mixer.init()\n",
197
+ " \n",
198
+ " # 加载音乐\n",
199
+ " pygame.mixer.music.load(path[0])\n",
200
+ " \n",
201
+ " # 播放音乐\n",
202
+ " pygame.mixer.music.play()"
203
+ ]
204
+ },
205
+ {
206
+ "cell_type": "markdown",
207
+ "id": "87ee0b38",
208
+ "metadata": {},
209
+ "source": [
210
+ "## GUI界面"
211
+ ]
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "execution_count": null,
216
+ "id": "7d3a5f55",
217
+ "metadata": {},
218
+ "outputs": [],
219
+ "source": [
220
+ "import tkinter\n",
221
+ "from tkinter.filedialog import askopenfilenames\n",
222
+ "\n",
223
+ "\n",
224
+ "class PlayWindow:\n",
225
+ " def __init__(self, myService):\n",
226
+ " \"\"\"初始化\"\"\"\n",
227
+ " self.myService = myService\n",
228
+ " \n",
229
+ " self.listbox = None\n",
230
+ "\n",
231
+ " # 导入音乐\n",
232
+ " def impMusic(self, even):\n",
233
+ " \"\"\"导入音乐\"\"\"\n",
234
+ " print('点击了导入音乐按钮')\n",
235
+ " \n",
236
+ " # 打开磁盘选择音乐\n",
237
+ " files = askopenfilenames(filetype=([\"mp3\", \"*.mp3\"],))\n",
238
+ " \n",
239
+ " # 导入音乐,将选择的音乐保存到数据库\n",
240
+ " self.myService.add_music(files)\n",
241
+ " \n",
242
+ " # 刷新音乐列表\n",
243
+ " self.flush_list()\n",
244
+ "\n",
245
+ "\n",
246
+ " def flush_list(self):\n",
247
+ " \"\"\"刷新当前用户的音乐列表\"\"\"\n",
248
+ " # 清空列表\n",
249
+ " self.listbox.delete(0, tkinter.END)\n",
250
+ " \n",
251
+ " # 查询该用户的所有音乐列表\n",
252
+ " music_list = self.myService.findListByUser()\n",
253
+ " \n",
254
+ " # 重新插入音乐\n",
255
+ " if music_list:\n",
256
+ " for m in music_list:\n",
257
+ " self.listbox.insert(tkinter.END, m)\n",
258
+ "\n",
259
+ "\n",
260
+ " def playMusic(self, even):\n",
261
+ " \"\"\"播放音乐\"\"\"\n",
262
+ " print('点击了播放音乐按钮')\n",
263
+ " \n",
264
+ " # 获取播放的音乐\n",
265
+ " index = self.listbox.curselection()\n",
266
+ " music_name = self.listbox.get(index)\n",
267
+ " \n",
268
+ " # 调用方法播放音乐\n",
269
+ " self.myService.playMuisc(music_name)\n",
270
+ "\n",
271
+ "\n",
272
+ " def deleteMusic(self, even):\n",
273
+ " \"\"\"删除音乐\"\"\"\n",
274
+ " print('点击了删除音乐按钮')\n",
275
+ " \n",
276
+ " # 获取删除音乐的名称\n",
277
+ " index = self.listbox.curselection()\n",
278
+ " music_name = self.listbox.get(index)\n",
279
+ " print(music_name)\n",
280
+ " \n",
281
+ " # 调用删除方法删除音乐\n",
282
+ " self.myService.delete_music(music_name)\n",
283
+ " \n",
284
+ " # 刷新列表\n",
285
+ " self.flush_list()\n",
286
+ "\n",
287
+ " def showWindow(self):\n",
288
+ " \"\"\"展示窗口\"\"\"\n",
289
+ " \n",
290
+ " # 根窗口对象\n",
291
+ " top = tkinter.Tk()\n",
292
+ "\n",
293
+ " # 创建按钮\n",
294
+ " play_button = tkinter.Button(top, text=\"播放\")\n",
295
+ " imp_button = tkinter.Button(top, text=\"导入音乐\")\n",
296
+ " delete_button = tkinter.Button(top, text=\"删除\")\n",
297
+ " \n",
298
+ " # 对按钮进行布局\n",
299
+ " play_button.grid(row=0, column=0, padx=5, pady=5)\n",
300
+ " imp_button.grid(row=0, column=2, padx=5, pady=5)\n",
301
+ " delete_button.grid(row=0, column=4, padx=5, pady=5)\n",
302
+ "\n",
303
+ " # 添加列表\n",
304
+ " self.listbox = tkinter.Listbox(top)\n",
305
+ " self.listbox.grid(row=1, column=0, padx=5, pady=5, columnspan=9)\n",
306
+ "\n",
307
+ " # 给按钮添加点击事件\n",
308
+ " imp_button.bind(\"<ButtonRelease-1>\", self.impMusic)\n",
309
+ " play_button.bind(\"<ButtonRelease-1>\", self.playMusic)\n",
310
+ " delete_button.bind(\"<ButtonRelease-1>\", self.deleteMusic)\n",
311
+ "\n",
312
+ " # 刷新音乐列表\n",
313
+ " self.flush_list()\n",
314
+ "\n",
315
+ " # 展示GUI\n",
316
+ " top.mainloop()\n",
317
+ "\n",
318
+ "\n",
319
+ "if __name__ == \"__main__\":\n",
320
+ " \n",
321
+ " # 输入登录信息\n",
322
+ " uname = input(\"请输入用户名:\")\n",
323
+ " password = input(\"请输入密码:\")\n",
324
+ " \n",
325
+ " # 创建提供服务类\n",
326
+ " myservice = MyService()\n",
327
+ " \n",
328
+ " # 登录成功\n",
329
+ " if myservice.login(uname, password):\n",
330
+ " print('登录成功')\n",
331
+ " \n",
332
+ " # 创建 GUI\n",
333
+ " playWindow = PlayWindow(myservice)\n",
334
+ " \n",
335
+ " # 展示\n",
336
+ " playWindow.showWindow()\n",
337
+ "\n",
338
+ " # 登录失败\n",
339
+ " else:\n",
340
+ " print('登录失败')"
341
+ ]
342
+ }
343
+ ],
344
+ "metadata": {
345
+ "kernelspec": {
346
+ "display_name": "Python 3 (ipykernel)",
347
+ "language": "python",
348
+ "name": "python3"
349
+ },
350
+ "language_info": {
351
+ "codemirror_mode": {
352
+ "name": "ipython",
353
+ "version": 3
354
+ },
355
+ "file_extension": ".py",
356
+ "mimetype": "text/x-python",
357
+ "name": "python",
358
+ "nbconvert_exporter": "python",
359
+ "pygments_lexer": "ipython3",
360
+ "version": "3.9.12"
361
+ },
362
+ "toc": {
363
+ "base_numbering": 1,
364
+ "nav_menu": {},
365
+ "number_sections": true,
366
+ "sideBar": true,
367
+ "skip_h1_title": false,
368
+ "title_cell": "Table of Contents",
369
+ "title_sidebar": "Contents",
370
+ "toc_cell": false,
371
+ "toc_position": {
372
+ "height": "calc(100% - 180px)",
373
+ "left": "10px",
374
+ "top": "150px",
375
+ "width": "288px"
376
+ },
377
+ "toc_section_display": true,
378
+ "toc_window_display": true
379
+ }
380
+ },
381
+ "nbformat": 4,
382
+ "nbformat_minor": 5
383
+ }
03_Redis数据库概述.ipynb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bf4172a08d7804cf35b045472f45eefb5cf64e2a03a7b9c25dabc9bcb736600b
3
+ size 37642843