Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ce1069c

Browse files
committed
Python sandman2
1 parent fa48fe7 commit ce1069c

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

‎taiyangxue/sandman2/app.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
"""
3+
创建mysql 数据库,将 sandman2_test_data.sql 导入
4+
5+
启动 sandman 服务器
6+
7+
sandman2ctl 'mysql+pymysql://bob:bobpasswd@localhost:3306/sandman2_test_data'
8+
"""
9+
10+
import requests as rq
11+
import json
12+
13+
## 返回 学生表 student 的所有记录
14+
# curl http://localhost:5000/student/
15+
16+
data = rq.get("http://localhost:5000/student/").content
17+
data = json.loads(data)
18+
print("student 的所有记录")
19+
print(data)
20+
21+
## 返回 学生表 student 的第一页数据
22+
# curl http://localhost:5000/student/?page=1
23+
data = rq.get("http://localhost:5000/student/?page=1").content
24+
data = json.loads(data)
25+
print("student 的第一页数据")
26+
print(data)
27+
28+
## 获取 id 为 1 的学生记录
29+
# curl http://localhost:5000/student/1
30+
data = rq.get("http://localhost:5000/student/1").content
31+
data = json.loads(data)
32+
print("id 为 1 的学生记录")
33+
print(data)
34+
35+
## 查询 `name` 为 Tom 的学生记录
36+
# curl http://localhost:5000/student/?name=Tom
37+
data = rq.get("http://localhost:5000/student/?name=Tom").content
38+
data = json.loads(data)
39+
print("查询 `name` 为 Tom 的学生记录")
40+
print(data)
41+
42+
## 查询班级为 1 年龄为 18 的学生:
43+
# curl http://localhost:5000/student/?class=1&age=19
44+
data = rq.get("http://localhost:5000/student/?class=1&age=19").content
45+
data = json.loads(data)
46+
print("查询班级为 1 年龄为 18 的学生")
47+
print(data)
48+
49+
## 增加一个学生信息
50+
# curl -X POST -d '{"name": "Lily", "age": 17, "class":1, "profile":"Likely"}' -H "Content-Type: application/json" http://127.0.0.1:5000/student/
51+
data = rq.post("http://127.0.0.1:5000/student/", headers={"Content-Type": "application/json"}, data='{"name": "Tiger", "age": 17, "class":2, "profile":"Handsame"}').content
52+
data = json.loads(data)
53+
print("增加一个学生信息")
54+
print(data)
55+
56+
## id 为 1 的学生班级更改为 3
57+
# curl -X PATCH -d '{"class":3}' -H "Content-Type: application/json" http://127.0.0.1:5000/student/1
58+
data = rq.patch("http://127.0.0.1:5000/student/1", headers={"Content-Type": "application/json"}, data='{"class": 3}').content
59+
data = json.loads(data)
60+
print("id 为 1 的学生班级更改为 3")
61+
print(data)
62+
63+
## 删除 id 为 11 的学生记录
64+
# curl -X DELETE -H "Content-Type: application/json" http://127.0.0.1:5000/student/11
65+
data = rq.delete("http://127.0.0.1:5000/student/11").content
66+
print("删除 id 为 11 的学生记录")
67+
print(data)
68+
69+
## 获取 学生表 student 的字段定义
70+
# curl http://127.0.0.1:5000/student/meta
71+
data = rq.get("http://localhost:5000/student/meta").content
72+
data = json.loads(data)
73+
print("获取 学生表 student 的字段定义")
74+
print(data)
75+
76+
## 导出学生数据,存放到 student.csv 文件中
77+
# curl -o student.csv http://127.0.0.1:5000/student/?export
78+
r = rq.get("http://127.0.0.1:5000/student/?export")
79+
with open("student.csv",'wb') as f:
80+
f.write(r.content)
81+
print("数据导出完毕")
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
SET NAMES utf8mb4;
2+
SET FOREIGN_KEY_CHECKS = 0;
3+
4+
-- ----------------------------
5+
-- Table structure for course
6+
-- ----------------------------
7+
DROP TABLE IF EXISTS `course`;
8+
CREATE TABLE `course` (
9+
`id` int(11) NOT NULL,
10+
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
11+
`year` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
12+
`grade` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
13+
PRIMARY KEY (`id`) USING BTREE
14+
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
15+
16+
-- ----------------------------
17+
-- Records of course
18+
-- ----------------------------
19+
INSERT INTO `course` VALUES (1, '语文', '2020', '1');
20+
INSERT INTO `course` VALUES (2, '数学', '2020', '1');
21+
INSERT INTO `course` VALUES (3, '体育', '2020', '1');
22+
INSERT INTO `course` VALUES (4, '英语', '2020', '1');
23+
24+
-- ----------------------------
25+
-- Table structure for student
26+
-- ----------------------------
27+
DROP TABLE IF EXISTS `student`;
28+
CREATE TABLE `student` (
29+
`id` int(11) NOT NULL AUTO_INCREMENT,
30+
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
31+
`class` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
32+
`age` int(11) NULL DEFAULT NULL,
33+
`profile` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
34+
PRIMARY KEY (`id`) USING BTREE
35+
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
36+
37+
-- ----------------------------
38+
-- Records of student
39+
-- ----------------------------
40+
INSERT INTO `student` VALUES (1, '张三', '3', 18, '擅长学习');
41+
INSERT INTO `student` VALUES (2, '李四', '1', 19, '喜欢篮球');
42+
INSERT INTO `student` VALUES (3, '王五', '2', 18, '爱编程');
43+
INSERT INTO `student` VALUES (4, '赵六', '2', 20, '写作能手');
44+
INSERT INTO `student` VALUES (6, 'Bob', '2', 19, 'Good Boy');
45+
INSERT INTO `student` VALUES (7, 'Tom', '1', 19, 'Handsome');
46+
INSERT INTO `student` VALUES (9, 'Lily', '1', 17, 'Likely too');
47+
48+
-- ----------------------------
49+
-- Table structure for student_course
50+
-- ----------------------------
51+
DROP TABLE IF EXISTS `student_course`;
52+
CREATE TABLE `student_course` (
53+
`id` int(11) NOT NULL,
54+
`student_id` int(11) NULL DEFAULT NULL,
55+
`course_id` int(11) NULL DEFAULT NULL,
56+
`createtime` datetime(0) NULL DEFAULT NULL,
57+
PRIMARY KEY (`id`) USING BTREE
58+
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
59+
60+
-- ----------------------------
61+
-- Records of student_course
62+
-- ----------------------------
63+
64+
SET FOREIGN_KEY_CHECKS = 1;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /