目录
一、实现效果(视频演示)
二、代码的简单介绍
本博客实现了一个简单的车辆管理系统,包括两个主要角色:管理员(Manager)和租车人(Renter)。
管理员可以添加车辆、修改车辆信息、查询所有车辆以及查询指定车辆信息。
租车人可以查询车辆信息、租借车辆以及归还车辆。
三、详细代码
Bus.py
from Car import Car
class Bus(Car):
bus_count = 0
def __init__(self, sedan_id, brand="", bus_loads=30, day_charge=400, deposit=2000,
renter="", renter_contact="", data_rent="", data_return="",rented=False):
super().__init__(sedan_id, rented=rented)
self.brand = brand
self.bus_loads = bus_loads
self.day_charge = day_charge
self.deposit = deposit
self.renter = renter
self.renter_contact = renter_contact
self.data_rent = data_rent
self.data_return = data_return
Bus.bus_count += 1
def show_msg(self):
if self.rented:
print(f"车牌:{self.car_id},车辆是否出租:{self.rented}", end=",")
print(f"日租金:{self.day_charge},押金:{self.deposit}", end=",")
print(f"租借人:{self.renter},租借人联系方式:{self.renter_contact}", end=",")
print(f"租借日期:{self.data_rent},归还日期:{self.data_return}")
else:
print(f"车牌:{self.car_id},车辆是否出租:False",end=",")
print(f"日租金:{self.day_charge},押金:{self.deposit}", end=",")
if __name__ == '__main__':
s1 = Bus("B001", brand="ford", bus_loads=40)
s1.show_msg()
Car.py
class Car:
car_count = 0
def __init__(self, car_id, day_charge=50, deposit=100, rented=False):
self.deposit = deposit
self.day_charge = day_charge
self.car_id = car_id
self.rented = rented
self.renter = ""
self.renter_contact = ""
self.data_rent = ""
self.data_return = ""
Car.car_count += 1
def show_msg(self):
if self.rented:
print(f"车牌:{self.car_id},车辆是否出租:{self.rented}", end=",")
print(f"日租金:{self.day_charge},押金:{self.deposit}", end=",")
print(f"租借人:{self.renter},租借人联系方式:{self.renter_contact}", end=",")
print(f"租借日期:{self.data_rent},归还日期:{self.data_return}")
else:
print(f"车牌:{self.car_id},车辆是否出租:False", end=",")
print(f"日租金:{self.day_charge},押金:{self.deposit}", end=",")
if __name__ == '__main__':
car1 = Car("C001")
print(f"已经有{car1.car_count}辆车")
car1.show_msg()
car_info
C001,False,BYD,5,50,200
C002,True,BYD,5,60,200,AAA,159,5-26,6-1
B001,False,Tesla,20,40,500
B002,True,Tesla,30,80,400,BBB,178,5-26,6-1
T001,False,50,60,6,5,4,90,800
T002,True,40,30,3,2,2,70,600,www,132,3-2,4-1
C004,False,BYD,7,400,2000
B004,False,WWW,80,600,4000
C003,False,BYD,5,600,2000
C005,False,BYD,7,300,2000
C006,False,BYD,5,300,
B003,False,QQQ,35,600,1000
C008,False,BYD,7,600,40000
T005,False,40,50,6,5,,100,2000
main-car.py
from Manage import Manager
from Renter import Renter
def main():
opinion = int(input("请选择登录类型(1.管理员,2.用户):"))
if opinion == 1:
manager = Manager()
elif opinion == 2:
renter = Renter()
if __name__ == '__main__':
main()
Manage.py
from Car import Car
from Sedan import Sedan
from Bus import Bus
from Truck import Truck
class Manager:
cars_list = []
def __init__(self):
print("Welcome Manage")
self.menu()
def menu(self):
while True:
self.load_info()
print("==================")
print(" 1 添加车辆")
print(" 2 查询车辆")
print(" 3 显示所有车辆")
print(" 4 修改车辆")
print(" 5 退出系统")
print("==================")
opinion = int(input("请输入您的选择项:"))
if opinion == 5:
print("5 退出系统")
exit()
if opinion == 1:
print("1 添加车辆")
self.addCar()
elif opinion == 2:
print("2 查询车辆")
self.searchCar()
elif opinion == 3:
print("3 显示所有车辆")
self.show_all_cars()
elif opinion == 4:
print("4 修改车辆")
self.changeCar()
else:
print("输入有误,请重新输入")
def load_info(self):
# 读取文件
global newCar
with open('Manage.txt', 'r') as file:
lines = file.readlines()
for line in lines:
temp = line.split(',')
if "C" in temp[0]:
car_type = temp[0]
rented = temp[1]
brand = temp[2]
seats = temp[3]
day_charge = temp[4]
deposit = temp[5]
newCar = Sedan(car_type, rented, brand, seats)
if rented == True:
renter = temp[6]
renter_contact = temp[7]
data_rent = temp[8]
data_return = temp[9].strip()
newCar = Sedan(car_type, rented, brand, seats, day_charge, deposit, renter,
renter_contact, data_rent, data_return)
Manager.cars_list.append(newCar)
if "B" in temp[0]:
car_type = temp[0]
rented = temp[1]
brand = temp[2]
bus_loads = temp[3]
day_charge = temp[4]
deposit = temp[5]
newCar = Bus(car_type, rented, brand, bus_loads)
if rented == True:
renter = temp[6]
renter_contact = temp[7]
data_rent = temp[8]
data_return = temp[9].strip()
newCar = Bus(car_type, rented, brand, bus_loads, day_charge, deposit, renter,
renter_contact, data_rent, data_return)
Manager.cars_list.append(newCar)
if "T" in temp[0]:
car_type = temp[0]
rented = temp[1]
volume = temp[2]
capacity = temp[3]
length = temp[4]
width = temp[5]
height = temp[6]
day_charge = temp[7]
deposit = temp[8]
newCar = Truck(car_type, rented, volume, capacity, length, width, height)
if rented == True:
renter = temp[9]
renter_contact = temp[10]
data_rent = temp[11]
data_return = temp[12].strip()
newCar = Truck(car_type, rented, volume, capacity, length, width, height,
day_charge, deposit, renter, renter_contact, data_rent, data_return)
Manager.cars_list.append(newCar)
def addCar(self):
global rented, brand, seats, deposit, data_return, data_rent, renter_contact, day_charge, renter, newCar, bus_loads, volume, length, capacity, width, height
while True:
opinion = int(input("请选择你要添加的车辆类型1.轿车 2.客车 3.货车:"))
print("车牌开头按车辆类型区分,C代表轿车,B代表客车,T代表货车")
car_type = input("请输入车牌:")
for car in Manager.cars_list:
if car.car_id == car_type:
print(f"车辆{car.car_id}已经存在,请重新输入:")
return
rent_opinion = int(input("车辆是否已经出租1.出租 2.未出租:"))
if rent_opinion == 1:
rented = True
elif rent_opinion == 2:
rented = False
if opinion == 1: ###轿车
if rent_opinion == 1:
brand = input("请输入轿车品牌:")
seats = str(input("请输入轿车座位数:"))
day_charge = str(input("请输入日租金"))
deposit = str(input("请输入押金"))
renter = input("请输入租借人")
renter_contact = input("请输入租借人联系方式")
data_rent = input("请输入租借日期")
data_return = input("请输入归还日期")
newCar = Sedan(car_type, rented, brand, seats, day_charge, deposit, renter,
renter_contact, data_rent, data_return)
with open('Manage.txt', 'a') as file:
file.write(f"{car_type},{rented},{brand},{seats},{day_charge},{deposit},{renter},{renter_contact},{data_rent},{data_return}\n")
else:
brand = input("请输入轿车品牌:")
seats = str(input("请输入轿车座位数:"))
day_charge = str(input("请输入日租金"))
deposit = str(input("请输入押金"))
rented = False
newCar = Sedan(car_type, rented, brand, seats)
with open('Manage.txt', 'a') as file:
file.write(f"{car_type},{rented},{brand},{seats},{day_charge},{deposit}\n")
Manager.cars_list.append(newCar)
elif opinion == 2: ###客车
bus_loads = input("请输入客车载客量:")
if rent_opinion == 1:
brand = input("请输入客车品牌:")
day_charge = str(input("请输入日租金"))
deposit = str(input("请输入押金"))
renter = input("请输入租借人")
renter_contact = input("请输入租借人联系方式")
data_rent = input("请输入租借日期")
data_return = input("请输入归还日期")
newCar = Bus(car_type, rented, brand, bus_loads, day_charge, deposit, renter,
renter_contact, data_rent, data_return)
with open('Manage.txt', 'a') as file:
file.write(f"{car_type},{rented},{brand},{bus_loads},{day_charge},{deposit},{renter},{renter_contact},{data_rent},{data_return}\n")
else:
brand = input("请输入客车品牌:")
seats = str(input("请输入客车座位数:"))
day_charge = str(input("请输入日租金"))
deposit = str(input("请输入押金"))
newCar = Bus(car_type, rented, brand, bus_loads)
with open('Manage.txt','a') as file:
file.write(f"{car_type},{rented},{brand},{bus_loads},{day_charge},{deposit}\n")
Manager.cars_list.append(newCar)
elif opinion == 3: ###货车
if rent_opinion == 1:
volume = str(input("请输入载货体积"))
capacity = str(input("请输入载货重量"))
length = str(input("请输入长"))
width = str(input("请输入宽"))
height = str(input("请输入高"))
day_charge = str(input("请输入日租金"))
deposit = str(input(("请输入押金")))
renter = input("请输入租借人")
renter_contact = input("请输入租借人联系方式")
data_rent = input("请输入租借日期")
data_return = input("请输入归还日期")
newCar = Truck(car_type, rented, volume, capacity, length, width, height,
day_charge, deposit, renter, renter_contact, data_rent, data_return)
with open('Manage.txt', 'a') as file:
file.write(f"{car_type},{rented},{volume},{capacity},{length},{width},{height},{day_charge},{deposit},{renter},{renter_contact},{data_rent},{data_return}\n")
else:
volume = input("请输入载货体积")
capacity = input("请输入载货重量")
length = input("请输入长")
width = input("请输入宽")
height = input("请输入高")
day_charge = input("请输入日租金")
deposit = input(("请输入押金"))
newCar = Truck(car_type, rented, volume, capacity, length, width, height)
with open('Manage.txt', 'a') as file:
file.write(f"{car_type},{rented},{volume},{capacity},{length},{width},{height},{day_charge},{deposit}\n")
Manager.cars_list.append(newCar)
print(f"车辆{newCar.car_id}添加成功")
flag = input("是否继续添加y/n:")
if flag != "y":
break
def searchCar(self):
while True:
flag_1 = 0
print("车牌开头按车辆类型区分,C代表轿车,B代表客车,T代表货车")
car_type_key = input("请输入您需要查找的车辆的车牌号:")
for car in Manager.cars_list:
flag_1 = 1
if car.car_id == car_type_key:
car.show_msg()
if flag_1 == 0:
print("输入有误,请重新输入")
flag = input("是否继续查找y/n:")
if flag != "y":
break
def show_all_cars(self):
while True:
print(f"已经有{Car.car_count}辆车")
for line in Manager.cars_list:
line.show_msg()
flag = input("是否继续查找y/n:")
if flag != "y":
break
def changeCar(self):
global brand, seats, deposit, day_charge, data_return, data_rent, renter_contact, renter, newCar, bus_loads, volume, capacity, length, height, width, rented, car_type
while True:
print("车牌开头按车辆类型区分,C代表轿车,B代表客车,T代表货车")
car_id_key = input("请输入您需要修改的车辆的车牌号:")
if "C" in car_id_key:
car_type = 1
elif "B" in car_id_key:
car_type = 2
elif "T" in car_id_key:
car_type = 3
pos = 0
for car in Manager.cars_list:
if car.car_id == car_id_key:
del (Manager.cars_list[pos])
if car_type == 1:
print("车牌开头按车辆类型区分,C代表轿车,B代表客车,T代表货车")
car_type = input("请输入车牌:")
brand = input("请输入轿车品牌:")
seats = str(input("请输入轿车座位数:"))
day_charge = input("请输入日租金:")
deposit = input("请输入押金:")
elif car_type == 2:
print("车牌开头按车辆类型区分,C代表轿车,B代表客车,T代表货车")
car_type = input("请输入车牌:")
brand = input("请输入客车品牌:")
seats = str(input("请输入客车座位数:"))
day_charge = input("请输入日租金:")
deposit = input("请输入押金:")
elif car_type == 3:
print("车牌开头按车辆类型区分,C代表轿车,B代表客车,T代表货车")
car_type = input("请输入车牌:")
brand = input("请输入货车品牌:")
volume = str(input("请输入载货体积"))
capacity = str(input("请输入载货重量"))
length = str(input("请输入长"))
width = str(input("请输入宽"))
height = str(input("请输入高"))
day_charge = input("请输入日租金:")
deposit = input("请输入押金:")
opinion = int(input("车辆是否已经出租1.出租 2.未出租:"))
if opinion == 1:
rented = True
elif opinion == 2:
rented = False
if rented:
renter = input("请输入租借人:")
renter_contact = input("请输入租借人联系方式:")
data_rent = input("请输入租借日期:")
data_return = input("请输入归还日期:")
if car_type == 1:
if opinion == 1:
newCar = Sedan(car_type, rented, brand, seats, day_charge, deposit, renter,
renter_contact, data_rent, data_return)
else:
newCar = Sedan(car_type, rented, brand, seats)
Manager.cars_list.insert(pos, newCar)
break
elif car_type == 2:
if opinion == 1:
newCar = Bus(car_type, rented, brand, day_charge, renter,
renter_contact, data_rent, data_return)
else:
newCar = Bus(car_type, rented, brand, bus_loads)
Manager.cars_list.insert(pos, newCar)
break
elif car_type == 3:
if opinion == 1:
newCar = Truck(car_type, rented, volume, capacity, length, width, height,
day_charge, deposit, renter, renter_contact, data_rent, data_return)
else:
newCar = Truck(car_type, rented, volume, capacity, length, width, height)
Manager.cars_list.insert(pos, newCar)
break
pos += 1
flag = input("是否继续修改y/n:")
if flag != "y":
break
if __name__ == '__main__':
manager = Manager()
Manage.txt
C001,False,BYD,5,50,200
C002,True,BYD,5,60,200,AAA,159,5-26,6-1
B001,False,Tesla,20,40,500
B002,True,Tesla,30,80,400,BBB,178,5-26,6-1
T001,False,50,60,6,5,4,90,800
T002,True,40,30,3,2,2,70,600,www,132,3-2,4-1
C004,False,BYD,7,400,2000
B004,False,WWW,80,600,4000
C003,False,BYD,5,600,2000
C005,False,BYD,7,300,2000
C006,False,BYD,5,300,
B003,False,QQQ,35,600,1000
C008,False,BYD,7,600,40000
T005,False,40,50,6,5,,100,2000
Renter.py
from Car import Car
from Sedan import Sedan
from Bus import Bus
from Truck import Truck
from Manage import Manager
class Renter:
cars = Manager.cars_list
with open('Manage.txt', 'r') as file:
str_temp = file.readlines()
def __init__(self):
print("Welcome Renter")
self.menu()
def menu(self):
while True:
self.load_info()
print("==================")
print(" 1 查询车辆")
print(" 2 租借车辆")
print(" 3 归还车辆")
print(" 4 退出系统")
print("==================")
opinion = int(input("请输入您的选择项:"))
if opinion == 1:
print("1 查询车辆")
self.searchCar()
elif opinion == 2:
print("2 租借车辆")
self.rentCar()
elif opinion == 3:
print("3 归还车辆")
self.returnCar()
elif opinion == 4:
print("4 退出系统")
exit()
else:
print("输入有误,请重新输入")
def load_info(self):
# 读取文件
global newCar
with open('Manage.txt', 'r') as file:
str_temp = file.readlines()
for line in str_temp:
temp = line.split(',')
if "C" in temp[0]:
car_type = temp[0]
rented = temp[1]
brand = temp[2]
seats = temp[3]
day_charge = temp[4]
deposit = temp[5]
newCar = Sedan(car_type, rented, brand, seats)
if rented == True:
renter = temp[6]
renter_contact = temp[7]
data_rent = temp[8]
data_return = temp[9].strip()
newCar = Sedan(car_type, rented, brand, seats, day_charge, deposit, renter,
renter_contact, data_rent, data_return)
Manager.cars_list.append(newCar)
if "B" in temp[0]:
car_type = temp[0]
rented = temp[1]
brand = temp[2]
bus_loads = temp[3]
day_charge = temp[4]
deposit = temp[5]
newCar = Bus(car_type, rented, brand, bus_loads)
if rented == True:
renter = temp[6]
renter_contact = temp[7]
data_rent = temp[8]
data_return = temp[9].strip()
newCar = Bus(car_type, rented, brand, bus_loads, day_charge, deposit, renter,
renter_contact, data_rent, data_return)
Manager.cars_list.append(newCar)
if "T" in temp[0]:
car_type = temp[0]
rented = temp[1]
volume = temp[2]
capacity = temp[3]
length = temp[4]
width = temp[5]
height = temp[6]
day_charge = temp[7]
deposit = temp[8]
newCar = Truck(car_type, rented, volume, capacity, length, width, height)
if rented == True:
renter = temp[9]
renter_contact = temp[10]
data_rent = temp[11]
data_return = temp[12].strip()
newCar = Truck(car_type, rented, volume, capacity, length, width, height,
day_charge, deposit, renter, renter_contact, data_rent, data_return)
Manager.cars_list.append(newCar)
def searchCar(self):
print("车牌开头按车辆类型区分,C代表轿车,B代表客车,T代表货车")
car_id_key = input("请输入您需要查找的车辆的车牌号:")
for car in Manager.cars_list:
if car.car_id == car_id_key:
Car.show_msg(car)
def rentCar(self):
global car
while True:
print("车牌开头按车辆类型区分,C代表轿车,B代表客车,T代表货车")
car_id_key = input("请输入您需要租借的车辆的车牌号:")
flag = 0
for car in Manager.cars_list:
if car.car_id == car_id_key:
flag = 1
if car.rented == "True":
print("该车已出租")
break
else:
car.rented = True
car.renter = input("请输入租借人:")
car.renter_contact = str(input("请输入租借人联系方式:"))
car.data_rent = str(input("请输入租借日期:"))
car.data_return = "待归还"
car.show_msg()
break
if flag == 1:
flag = 0
for key in Renter.str_temp:
if car_id_key in key:
if "S" in car_id_key:
Renter.str_temp[flag] = f"{car.car_id},{car.rented},{car.brand},{car.seats}," \
f"{car.day_charge},{car.deposit},{car.renter}," \
f"{car.renter_contact},{car.data_rent},待归还\n"
if "B" in car_id_key:
Renter.str_temp[flag] = f"{car.car_id},{car.rented},{car.brand},{car.bus_loads}," \
f"{car.day_charge},{car.deposit},{car.renter}," \
f"{car.renter_contact},{car.data_rent},待归还\n"
if "T" in car_id_key:
Renter.str_temp[flag] = f"{car.car_id},{car.rented},{car.volume},{car.capacity}," \
f"{car.length},{car.width},{car.height}," \
f"{car.day_charge},{car.deposit},{car.renter}," \
f"{car.renter_contact},{car.data_rent},待归还\n"
with open('Manage.txt', 'w') as file:
for temp in Renter.str_temp:
file.write(temp)
break
flag += 1
else:
print("未找到该车")
flag = input("是否继续租借y/n")
if flag != "y":
break
def returnCar(self):
global car
print("车牌开头按车辆类型区分,C代表轿车,B代表客车,T代表货车")
car_id_key = input("请输入您需要归还的车辆的车牌号:")
flag = 0
for car in Manager.cars_list:
if car.car_id == car_id_key:
flag = 1
if car.rented:
car.rented = False
car.data_return = input("请输入归还日期:")
car.show_msg()
break
if flag == 1:
flag = 0
for key in Renter.str_temp:
if car_id_key in key:
if "S" in car_id_key:
Renter.str_temp[flag] = f"{car.car_id},{car.rented},{car.brand},{car.seats}," \
f"{car.day_charge},{car.deposit},{car.renter}," \
f"{car.renter_contact},{car.data_rent},{car.data_return}\n"
if "B" in car_id_key:
Renter.str_temp[flag] = f"{car.car_id},{car.rented},{car.brand},{car.bus_loads}," \
f"{car.day_charge},{car.deposit},{car.renter}," \
f"{car.renter_contact},{car.data_rent},{car.data_return}\n"
if "T" in car_id_key:
Renter.str_temp[flag] = f"{car.car_id},{car.rented},{car.volume},{car.capacity}," \
f"{car.length},{car.width},{car.height}," \
f"{car.day_charge},{car.deposit},{car.renter}," \
f"{car.renter_contact},{car.data_rent},{car.data_return}\n"
with open('car_info', 'w') as file:
for temp in Renter.str_temp:
file.write(temp)
break
flag += 1
else:
print("未找到该车!")
if __name__ == '__main__':
renter = Renter()
Sedan.py
from Car import Car
import sqlite3
con = sqlite3.connect('Car.py')
class Sedan(Car):
sedan_count = 0
def __init__(self, sedan_id, rented=False, brand="", seats=5, day_charge=200, deposit=1000,
renter="", renter_contact="", data_rent="", data_return=""):
super().__init__(sedan_id, rented=rented)
self.brand = brand
self.seats = seats
self.day_charge = day_charge
self.deposit = deposit
self.renter = renter
self.renter_contact = renter_contact
self.data_rent = data_rent
self.data_return = data_return
Sedan.sedan_count += 1
def show_msg(self):
if self.rented:
print(f"车牌:{self.car_id},车辆是否出租:{self.rented}", end=",")
print(f"日租金:{self.day_charge},押金:{self.deposit}", end=",")
print(f"租借人:{self.renter},租借人联系方式:{self.renter_contact}", end=",")
print(f"租借日期:{self.data_rent},归还日期:{self.data_return}")
else:
print(f"车牌:{self.car_id},车辆是否出租:False", end=",")
print(f"日租金:{self.day_charge},押金:{self.deposit}", end=",")
if __name__ == '__main__':
s1 = Sedan("S001", brand="ford", seats=7)
s1.show_msg()
Truck.py
from Car import Car
class Truck(Car):
truck_count = 0
def __init__(self, sedan_id, volume="20方", capacity="5吨", length="5米", width="2米", height="2米",
day_charge=400, deposit=2000, renter="", renter_contact="", data_rent="", data_return="",rented=False):
super().__init__(sedan_id, rented=rented)
self.volume = volume
self.capacity = capacity
self.length = length
self.width = width
self.height = height
self.day_charge = day_charge
self.deposit = deposit
self.renter = renter
self.renter_contact = renter_contact
self.data_rent = data_rent
self.data_return = data_return
Truck.truck_count += 1
def show_msg(self):
if self.rented:
print(f"载货体积:{self.volume},载货重量:{self.capacity}", end=",")
print(f"长:{self.length},宽:{self.width},高:{self.height}")
print(f"车牌:{self.car_id},车辆是否出租:{self.rented}", end=",")
print(f"日租金:{self.day_charge},押金:{self.deposit}", end=",")
print(f"租借人:{self.renter},租借人联系方式:{self.renter_contact}", end=",")
print(f"租借日期:{self.data_rent},归还日期:{self.data_return}")
else:
print(f"载货体积:{self.volume},载货重量:{self.capacity}", end=",")
print(f"长:{self.length},宽:{self.width},高:{self.height}")
print(f"车牌:{self.car_id},车辆是否出租:False",end=",")
print(f"日租金:{self.day_charge},押金:{self.deposit}", end=",")
if __name__ == '__main__':
s1 = Truck("T001", volume="21方", capacity="4吨", length="5米", width="2.1米", height="2米")
s1.show_msg()
数据库实现租车.py
import sqlite3
def opendb():
conn = sqlite3.connect("mydb.db")
cur = conn.cursor()
cur = conn.execute(
"""create table if not exists car(car_id primary key,rented ,day_charge ,
deposit )""")
return cur, conn
# 查询所有信息
def showalldb():
print("-------------处理后数据--------------")
hel = opendb()
cur = hel[1].cursor()
cur.execute("select*from car")
res = cur.fetchall()
for line in res:
for h in line:
print(h)
print()
cur.close()
# 输入信息
def into():
car_id = input("请输入车牌")
rented = input("请输入租借情况")
day_charge = input("请输入日租金")
deposit = input("请输入押金")
return car_id, rented, day_charge, deposit
# 网格数据库中添加内容
def adddb():
welcome = """------欢迎使用数据库功能--------"""
print(welcome)
person = into()
hel = opendb()
hel[1].execute("insert into car(car_id,rented,day_charge,deposit)values(?,?,?,?)",
(person[0], person[1], person[2], person[3]))
hel[1].commit()
print("--------恭喜你,数据添加成功---------")
showalldb()
hel[1].close()
# 修改数据库内容
def alter():
welcome = "---------欢迎使用修改数据库功能------"
print(welcome)
changechoice = input("请输入想要修改的车牌:")
hel = opendb()
person = into()
hel[1].execute(
"update car set car_id=?,rented=?, day_charge=?,deposit=?where car" + changechoice,(person[0], person[1], person[2], person[3]))
hel[1].commit()
showalldb()
hel[1].close()
# 查询数据
def searchdb():
welcome = "--------欢迎使用数据库查询功能-------"
print(welcome)
choice = input("请输入查询的方式,如(车牌,租借情况,日租金,押金)")
searlist = ['', '']
if choice == "车牌":
searlist[0] = "car_id"
searlist[1] = input("请输入车牌:")
elif choice == "租借情况":
searlist[0] = "rented"
searlist[1] = input("请输入租借情况")
elif choice == "日租金":
searlist[0] = "day_charge"
searlist[1] = input("请输入日租金")
elif choice == "押金":
searlist[0] = "deposit"
searlist[1]=input("请输入押金")
hel = opendb()
cur = hel[1].cursor()
cur.execute("select*from car where " + searlist[0] + "=" + searlist[1])
hel[1].commit()
print("--------查询的数据如下------")
for row in cur:
print(row[0], row[1], row[2], row[3])
cur.close()
hel[1].close()
if __name__ == '__main__':
while True:
print("==================")
print(" 1 添加车辆")
print(" 2 查询车辆")
print(" 3 显示所有车辆")
print(" 4 修改车辆")
print(" 5 退出系统")
print("==================")
opinion = int(input("请输入您的选择项:"))
if opinion == 5:
print("5 退出系统")
exit()
if opinion == 1:
print("1 添加车辆")
adddb()
elif opinion == 2:
print("2 查询车辆")
searchdb()
elif opinion == 3:
print("3 显示所有车辆")
showalldb()
elif opinion == 4:
print("4 修改车辆")
alter()
else:
print("输入有误,请重新输入")