那就直接上代码
# 编写时间2021/6/9;16:01
# 编写 :刘钰琢
import MySQLdb
class chepiao():
def __init__(self, ch_num, ch_start_ad, ch_end_ad, ch_start_time, ch_end_time, ch_length, ch_money, ch_speed):
self.ch_num = ch_num
self.ch_start_ad = ch_start_ad
self.ch_end_ad = ch_end_ad
self.ch_start_time = ch_start_time
self.ch_end_time = ch_end_time
self.ch_length = ch_length
self.ch_money = ch_money
self.ch_speed = ch_speed
class jilu():
def __init__(self, buy_id, ch_start_ad, ch_end_ad, ch_length, p_name, ch_money, ch_num):
self.buy_id = buy_id
self.ch_start_ad = ch_start_ad
self.ch_end_ad = ch_end_ad
self.ch_length = ch_length
self.p_name = p_name
self.ch_money = ch_money
self.ch_num = ch_num
def main():
sql_begin()
try:
buy_id = get_buy_id()[-1] + 1
except:
buy_id = 1
while (True):
menu()
answer_main = input("请问您要进行什么操作(1、购票,2、统计,3、打印,4、添加,其他、退出系统)")
if answer_main == '1' or answer_main == '购票':
buy(buy_id)
buy_id += 1
elif answer_main == '2' or answer_main == '统计':
print_liuliang()
elif answer_main == '3' or answer_main == '打印':
print_piaoju()
elif answer_main == '4' or answer_main == '添加':
while (True):
user_password=input("请输入管理员密码")
if user_password=='lyz':
add()
break
else:
continue
else:
answer_quit = input('您是否要退出?y/n?')
if answer_quit == 'y' or answer_quit == 'Y':
break
else:
continue
def menu():
print('---------------------------------')
print('--------欢迎使用车票订购系统--------')
print('--------1、购票-------------------')
print('--------2、统计进站和出站流量-------')
print('--------3、打印票据信息------------')
print('--------4、添加票据和车次-----------')
def buy(id): # 购票
global str
p_name = input("请输入您的姓名")
buy_id = id
end_ad = input("请输入您要去的地方")
buy_sql = "select * from chepiao where ch_end_ad='" + end_ad + "'";
data = sql_chepiao(buy_sql, 'r')
if data == ():
print("还没有去这个地方的车辆,请联系管理员添加")
else:
i = 0
print('这是车票的信息')
for items in data:
print('这是第{0}辆车的车次信息'.format(i + 1))
for item in items:
print(item, end='\t')
print()
while (True):
answer_buy = int(input("请输入您要选择的车次信息"))
if answer_buy > i + 1:
print("您输入的车辆编号有误请重新输入")
continue
else:
break
chepiao_buy = data[answer_buy - 1]
# ('001', '郑州', '北京', '08:00', '10:25', 725, '309', 'G')
ch_money, ch_num, ch_start_ad, ch_end_ad, ch_length = chepiao_buy[6], chepiao_buy[0], chepiao_buy[1], \
chepiao_buy[
2], chepiao_buy[5]
lst = [buy_id, ch_start_ad, ch_end_ad, ch_length, p_name, ch_money, ch_num]
# buy_id,ch_start_ad,ch_end_ad,ch_length,p_name,ch_money ,ch_num
sql = "INSERT INTO jilu(buy_id,ch_start_ad,ch_end_ad,ch_length,p_name,ch_money ,ch_num)VALUES" + str(tuple(lst))
# print(sql)
sql_jilu(sql, 'w')
def print_liuliang(): # 输出流量
print("您是哟啊统计数据吗?")
end_ad = input("请输入您要统计的目的地")
sql = "SELECT * FROM `jilu` where ch_end_ad='" + end_ad + "'"
data = sql_chepiao(sql, 'r')
if data == ():
print("您输入的目的地并没有人去")
else:
i = 1
for items in data:
print("这是第{0}条数据:".format(i), end=' ')
i += 1
for item in items:
print(item, end='\t')
print('\n')
print('去{0}的人数共有{1}人'.format(end_ad, i - 1))
def print_piaoju(): # 打印票据
print("您是要打印的个人的凭据吗")
username = input('请输入您的姓名')
sql = "SELECT p_name,ch_money,buy_id,ch_num FROM `jilu` where p_name='" + username + "'"
data = sql_jilu(sql, 'r')
if data == ():
print("您并没有买过票")
else:
i = 1
for items in data:
print("这是第{0}条数据:".format(i), end=' ')
i += 1
for item in items:
print(item, end='\t')
print('\n')
def sql_chepiao(sql, design):
# 车票数据库信息
'''
create table chepiao(
ch_num CHAR(10),
ch_start_ad CHAR(10),
ch_end_ad CHAR(10),
ch_start_time char(10),
ch_end_time char(10),
ch_length int,
ch_money char(15),
ch_speed char(2)
);
'''
db = MySQLdb.connect("localhost", "root", "root", "chepiaodinggou", charset='utf8')
cursor = db.cursor()
cursor.execute(sql)
if design == 'r':
all_data = cursor.fetchall()
db.close()
print('读取完成')
return all_data
elif design == 'w':
db.commit()
print("添加完成")
db.close()
return None
def sql_jilu(sql, design):
'''
create table jilu(
buy_id int primary key,
ch_start_ad char(10),
ch_end_ad char(10),
ch_length int,
p_name char(20),
ch_money char(15),
ch_num char(10)
)
'''
db = MySQLdb.connect("localhost", "root", "root", "chepiaodinggou", charset='utf8')
cursor = db.cursor()
cursor.execute(sql)
if design == 'r':
all_data = cursor.fetchall()
db.close()
print('读取完成')
return all_data
elif design == 'w':
db.commit()
db.close()
print('添加完成')
return None
def get_buy_id():
sql = 'select buy_id from jilu '
data = sql_jilu(sql, 'r')
lst = []
for item in data:
lst.append(item[0])
lst.sort()
return lst
def add():
global buy_id, ch_start_ad, ch_length, ch_end_ad, p_name, ch_num, ch_money
while (True):
answer_add = input("请问您想添加那个的呢1、票据记录,2、车票,其他、退出")
if answer_add == '1' or answer_add == '票据记录':
ji = add_jilu()
lst1 = [ji.buy_id, ji.ch_start_ad, ji.ch_end_ad, ji.ch_length, ji.p_name, ji.ch_money, ji.ch_num]
# buy_id,ch_start_ad,ch_end_ad,ch_length,p_name,ch_money ,ch_num
sql = "INSERT INTO jilu(buy_id,ch_start_ad,ch_end_ad,ch_length,p_name,ch_money ,ch_num)VALUES" + str(
tuple(lst1))
sql_jilu(sql, 'w')
# sql_jilu(sql,'w')
elif answer_add == '2' or answer_add == '车票':
ch = add_chepiao()
lst2 = [ch.ch_num, ch.ch_start_ad, ch.ch_end_ad, ch.ch_start_time, ch.ch_end_time, ch.ch_length,
ch.ch_money, ch.ch_speed]
sql = "INSERT INTO chepiao(ch_num,ch_start_ad,ch_end_ad,ch_start_time,ch_end_time,ch_length,ch_money,ch_speed)VALUES" + str(
tuple(lst2))
sql_chepiao(sql, 'w')
# sql_chepiao(sql,'w')
else:
answer_add_quit = input("您确定要退出,不在添加了吗?y/n?")
if answer_add_quit == 'y' or answer_add_quit == 'Y':
break
else:
continue
def add_jilu():
global buy_id, ch_start_ad, ch_length, ch_end_ad, p_name, ch_num, ch_money
while (True):
print('请输入你要添加的内容,请不要留有空格')
buy_id = input("票据编号")
if buy_id == '':
continue
buy_id = int(buy_id)
ch_start_ad = input("开始地")
if ch_start_ad == '':
continue
ch_end_ad = input("目的地")
if ch_end_ad == '':
continue
ch_length = int(input("距离"))
if ch_length == '':
continue
p_name = input("订票人")
if p_name == '':
continue
ch_money = input("票价")
if ch_money == '':
continue
ch_num = input("车次号")
if ch_num == '':
continue
break
ji = jilu(buy_id, ch_start_ad, ch_end_ad, ch_length, p_name, ch_money, ch_num)
return ji
def add_chepiao():
global num, start_ad, end_ad, start_time, end_time, length, money, speed
while (True):
print('请输入你要添加的内容,请不要留有空格')
num = input("车辆编号")
if num == '':
continue
start_ad = input("开始地")
if start_ad == '':
continue
end_ad = input("目的地")
if end_ad == '':
continue
start_time = input("发车时间")
if start_time == '':
continue
end_time = input("到达时间")
if end_time == '':
continue
length = input("距离")
if length == '':
continue
length = int(length)
money = input("票价")
if money == '':
continue
speed = input("车辆类型")
if speed == '':
continue
break
ch = chepiao(num, start_ad, end_ad, start_time, end_time, length, money, speed)
return ch
def sql_begin():
sql = "select ch_num from chepiao"
data = sql_chepiao(sql, 'r')
# print(data)
data_true = (('001',), ('002',), ('003',), ('004',), ('005',), ('006',))
if data == data_true:
print("数据库里边已经有数据了")
return None
else:
end_ad_lst = []
beijing = chepiao('001', '郑州', '北京', '08:00', '10:25', 725, '309', 'G')
xian = chepiao('002', '郑州', '西安', '08:10', '10:20', 511, '221', 'G')
shijiazhuang = chepiao('003', '郑州', '石家庄', '08:09', '10:10', 427, '189', 'G')
jinan = chepiao('004', '郑州', '济南', '07:52', '12:11', 473, '303', 'G')
taiyuan = chepiao('005', '郑州', '太原', '07:10', '10:31', 509, '180', 'D')
wuhan = chepiao('006', '郑州', '武汉', '07:57', '09:55', 502, '244', 'G')
end_ad_lst.append(beijing)
end_ad_lst.append(xian)
end_ad_lst.append(shijiazhuang)
end_ad_lst.append(jinan)
end_ad_lst.append(taiyuan)
end_ad_lst.append(wuhan)
for item in end_ad_lst:
ch = item
lst2 = [ch.ch_num, ch.ch_start_ad, ch.ch_end_ad, ch.ch_start_time, ch.ch_end_time, ch.ch_length,
ch.ch_money,
ch.ch_speed]
sql = "INSERT INTO chepiao(ch_num,ch_start_ad,ch_end_ad,ch_start_time,ch_end_time,ch_length,ch_money,ch_speed)VALUES" + str(
tuple(lst2))
sql_chepiao(sql, 'w')
print("车次数据已经填入到数据库中")
if __name__ == '__main__':
main()