Python课的期末作业
要求:
1.上网查询郑州到北京,西安,石家庄,济南,太原,武汉的距离及票价,用数据库保存车次信息
2.要求输入目的地,能够查询到里程和票价
3.用数据库存储每一次售票记录,包括售票流水号,起点站,终点站,里程,金额等数据,并统计所有存储记录的总售票金额及各站的旅客流量(进站流量+出站流量)
4.能够打印票据信息,包括订票人信息,票价,票据号,车次等信息
import sqlite3
import os
import time
def createDB():
"""
创建并初始化数据库
"""
# 原始数据
info = [("G1564", "郑州", "北京", "309", "693"), ("G802", "郑州", "北京", "315", "693"), ("G564", "郑州", "北京", "326.5", "693"),
("G2025", "郑州", "西安", "239", "479.3"), ("D311", "郑州", "西安", "159", "479.3"), ("G857", "郑州", "西安", "229", "479.3"),
("G1286", "郑州", "石家庄", "189.5", "417.9"), ("G2070", "郑州", "石家庄", "196", "417.9"), ("G430", "郑州", "石家庄", "208", "417.9"),
("G2074", "郑州", "济南", "303", "446"), ("G258", "郑州", "济南", "312.5", "446"), ("G1844", "郑州", "济南", "298.5", "446"),
("D290", "郑州", "太原", "189", "432.7"), ("D3348", "郑州", "太原", "153", "432.7"), ("D2782", "郑州", "太原", "171", "432.7"),
("G1991", "郑州", "武汉", "244", "509.8"), ("G3203", "郑州", "武汉", "314", "509.8"), ("G8171", "郑州", "武汉", "248", "509.8")]
con = sqlite3.connect("DB.db")#连接到数据库
cur = con.cursor() # 创建游标对象
# 创建字段,num是车次,station是始发站,destination是目的地,price票价,distance是距离
cur.execute("create table test(num primary key, station, destination, price, distance)")
for i in info:
cur.execute("insert into test values(?,?,?,?,?)", i)
con.commit()
cur.close()
con.close()
def CreateSaleDB():
"""
创建销售数据库
"""
con = sqlite3.connect("sale.db")
cur = con.cursor()
# 字段依次为流水号,起点站,终点站,金额,里程
cur.execute("create table sheet(num,station,destination,price,distance)")
con.commit()
cur.close()
con.close()
# 根据目的地查询车票信息
def Query(dest):
con = sqlite3.connect("DB.db")
cur = con.cursor()
item = cur.execute("select * from test where destination=?", (dest,))
print("--------------------------查询到的信息--------------------------")
for i in item.fetchall():
print("车次:%s %s==>%s 票价:%s元 距离:%skm" % (i[0], i[1]