-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_data.py
More file actions
137 lines (126 loc) · 5.46 KB
/
generate_data.py
File metadata and controls
137 lines (126 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
文件名:generate_data.py
描述:使用mysql在本地连接,新建数据库book及相关表
"""
import pymysql
import traceback
host = 'tpe0.clusters.zeabur.com' # 127.0.0.1
port = 30872
user = 'root'
password = 'y498g30fkoN6' # 数据库密码
# 手动输入参数方式
# print('Starting the create database operation, please enter the information required for the database.')
# print('------------------------------------')
# host = input('please input database host:')
# port = input('please input database port:')
# user = input('please input database user:')
# password = input('please input database password:')
# print('------------------------------------')
try:
conn = pymysql.connect(host=host, port=int(port), user=user, password=password)
print('create database...')
cur = conn.cursor()
cur.execute('create database if not exists book')
print('database created done.')
print('------------------------------------')
conn.select_db('book')
print('create user table...')
cur.execute("CREATE TABLE IF NOT EXISTS user ("
"id varchar(50) PRIMARY KEY,"
"username varchar(255),"
"password varchar(255),"
"role int(11),"
"create_time datetime,"
"delete_flag int(11),"
"current_login_time datetime)")
print('user table created done.')
print('------------------------------------')
print('create book table...')
cur.execute("CREATE TABLE IF NOT EXISTS book("
"id varchar(50) PRIMARY KEY,"
"book_name varchar(255),"
"author varchar(255),"
"publish_company varchar(255),"
"store_number int(11),"
"borrow_number int(11),"
"create_time datetime,"
"publish_date date)")
print('book table created done.')
print('------------------------------------')
print('create borrow_info table...')
cur.execute("CREATE TABLE IF NOT EXISTS borrow_info ("
"id varchar(50) PRIMARY KEY,"
"book_id varchar(50),"
"book_name varchar(255),"
"borrow_user varchar(255),"
"borrow_num int(11),"
"borrow_days int(11),"
"borrow_time datetime,"
"return_time datetime,"
"return_flag int(11))")
print('borrow_info table created done.')
print('------------------------------------')
print('create message table...')
cur.execute("create table if not exists message ("
"id varchar(50) PRIMARY KEY,"
"sender_name varchar(255) NOT NULL,"
"receiver_name varchar(255) NOT NULL,"
"send_content varchar(255) NOT NULL,"
"send_time datetime,"
"is_replied int(11),"
"reply_content varchar(255),"
"reply_time datetime)")
print('message table created done.')
print('create annoucement table...')
cur.execute("create table if not exists annoucement ("
" `id` varchar(50) NOT NULL,"
"`annouce_title` varchar(255) NOT NULL,"
"`annouce_content` varchar(255) NOT NULL,"
" `annouce_time` datetime DEFAULT NULL,"
"PRIMARY KEY (`id`))")
print('annoucement table created done.')
print('create douban_book table...')
cur.execute("create table if not exists douban_book ("
' `img_href` varchar(255) DEFAULT NULL,'
' `title` varchar(255) DEFAULT NULL,'
' `author` varchar(255) DEFAULT NULL,'
'`pub` varchar(255) DEFAULT NULL,'
' `pub_year` varchar(255) DEFAULT NULL,'
' `price` varchar(255) DEFAULT NULL,'
' `grade` varchar(255) DEFAULT NULL,'
' `remark_num` varchar(255) DEFAULT NULL,'
' `quote` varchar(255) DEFAULT NULL)')
print('douban_book table created done.')
print('-' * 30)
print('operate done.')
print('create database successfully.')
except Exception as e:
print('the require information of database is correct, please check it and retry.')
print(e.args)
traceback.print_exc()
print('create database failed.')
print('Is insert some sample data into the database?')
print('1. insert')
print('2. exit')
insert_tag = input('please select the option:')
if insert_tag == '1':
# 新建两个用户,管理员admin和普通用户张三
print('------------------------------------')
print('starting the insert data operation...')
admin_data = ['12644064935811ea9063d8c497639e37', 'admin', '21232f297a57a5a743894a0e4a801fc3', 0,
'2021-06-01 15:23:12', 0, '2021-06-01 15:24:23']
user_data = ['99477a9e935811ea8171d8c497639e37', 'zhangsan', 'e10adc3949ba59abbe56e057f20f883e', 1,
'2021-06-03 15:23:12', 0, '2021-06-03 15:24:23']
sql = 'insert into user (id, username, password, role, create_time, delete_flag, current_login_time) values(%s,%s,' \
'%s,%s,%s,%s,%s)'
cur.execute(sql, admin_data)
cur.execute(sql, user_data)
conn.commit()
cur.close()
conn.close()
print('insert operation done.')
print('------------------------------------')
print('Now you can use admin account login with username="admin" password="admin" or use the normal account login'
'with username="zhangsan" password="123456".')
else:
print('system exit.')