0% found this document useful (0 votes)
12 views9 pages

MySQL Parking Management System

This document is a Python script that implements a parking management system using MySQL. It includes functions to create a database, manage parking spots, park vehicles, and calculate fees based on parking duration. The main function provides a user interface for interacting with the system through a command-line menu.

Uploaded by

ajayyadavme50
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

MySQL Parking Management System

This document is a Python script that implements a parking management system using MySQL. It includes functions to create a database, manage parking spots, park vehicles, and calculate fees based on parking duration. The main function provides a user interface for interacting with the system through a command-line menu.

Uploaded by

ajayyadavme50
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import mysql.

connector

from [Link] import Error

import date me

DB_HOST = "localhost"

DB_USER = "root"

DB_PASS = "system"

DB_NAME = "parking_db"

def create_connec on(database=None):

try:

kwargs = {"host": DB_HOST, "user": DB_USER, "password": DB_PASS}

if database:

kwargs["database"] = database

conn = [Link](**kwargs)

if conn.is_connected():

return conn

except Error as e:

print(f"DB Error: {e}")

return None

def create_database():

conn = create_connec on()

if not conn:
print("Cannot connect to MySQL server.")

return

try:

cur = [Link]()

[Link](f"CREATE DATABASE IF NOT EXISTS {DB_NAME}")

print("Database ready.")

except Error as e:

print(f"Create DB error: {e}")

finally:

[Link]()

def connect_db():

return create_connec on(database=DB_NAME)

def create_tables():

conn = connect_db()

if not conn:

return

try:

cur = [Link]()

[Link]("""

CREATE TABLE IF NOT EXISTS spots (

spot_id INT AUTO_INCREMENT PRIMARY KEY,

spot_no VARCHAR(30) NOT NULL,

is_available BOOLEAN NOT NULL DEFAULT TRUE

)
""")

[Link]("""

CREATE TABLE IF NOT EXISTS parkings (

parking_id INT AUTO_INCREMENT PRIMARY KEY,

spot_id INT,

vehicle_no VARCHAR(50),

owner_name VARCHAR(100),

park_ me DATETIME,

leave_ me DATETIME,

fee DECIMAL(8,2) DEFAULT 0,

FOREIGN KEY (spot_id) REFERENCES spots(spot_id)

""")

[Link]()

print("Tables ready.")

except Error as e:

print(f"Create tables error: {e}")

finally:

[Link]()

def add_spot(spot_no):

conn = connect_db()

if not conn:

return

try:

cur = [Link]()
[Link]("INSERT INTO spots (spot_no) VALUES (%s)", (spot_no,))

[Link]()

print("Spot added.")

except Error as e:

print(f"Add spot error: {e}")

finally:

[Link]()

def view_spots():

conn = connect_db()

if not conn:

return

try:

cur = [Link]()

[Link]("SELECT spot_id, spot_no, is_available FROM spots ORDER BY spot_id")

rows = [Link]()

if not rows:

print("No spots.")

return

for r in rows:

status = "Free" if r[2] else "Taken"

print(f"ID:{r[0]} No:{r[1]} Status:{status}")

except Error as e:

print(f"View spots error: {e}")

finally:

[Link]()
def park_vehicle(spot_id, vehicle_no, owner_name):

conn = connect_db()

if not conn:

return

try:

cur = [Link]()

[Link]("SELECT is_available FROM spots WHERE spot_id=%s", (spot_id,))

r = [Link]()

if not r:

print("Spot not found.")

return

if not r[0]:

print("Spot is already taken.")

return

[Link]("UPDATE spots SET is_available=FALSE WHERE spot_id=%s", (spot_id,))

[Link](

"INSERT INTO parkings (spot_id, vehicle_no, owner_name, park_ me) VALUES


(%s,%s,%s,%s)",

(spot_id, vehicle_no, owner_name, date [Link] [Link]())

[Link]()

print("Vehicle parked.")

except Error as e:

print(f"Park error: {e}")

finally:
[Link]()

def leave_vehicle(parking_id):

conn = connect_db()

if not conn:

return

try:

cur = [Link]()

[Link]("SELECT spot_id, park_ me, leave_ me FROM parkings WHERE


parking_id=%s", (parking_id,))

rec = [Link]()

if not rec:

print("Parking record not found.")

return

spot_id, park_ me, leave_ me = rec

if leave_ me is not None:

print("Already le .")

return

now = date [Link] [Link]()

# Fee: 20 per hour, par al hour charged as full, min 20

diff = now - (park_ me or now)

hours = int(diff.total_seconds() // 3600)

if diff.total_seconds() % 3600 != 0:

hours += 1

if hours < 1:

hours = 1
fee = 20 * hours

[Link]("UPDATE parkings SET leave_ me=%s, fee=%s WHERE parking_id=%s", (now,


fee, parking_id))

[Link]("UPDATE spots SET is_available=TRUE WHERE spot_id=%s", (spot_id,))

[Link]()

print(f"Vehicle le . Fee: Rs. {fee}")

except Error as e:

print(f"Leave error: {e}")

finally:

[Link]()

def view_parkings():

conn = connect_db()

if not conn:

return

try:

cur = [Link]()

[Link]("SELECT parking_id, spot_id, vehicle_no, owner_name, park_ me, leave_ me,


fee FROM parkings ORDER BY parking_id")

rows = [Link]()

if not rows:

print("No parking records.")

return

for r in rows:

out = r[5] if r[5] else "-"

fee = r[6] if r[6] else 0

print(f"PID:{r[0]} Spot:{r[1]} Veh:{r[2]} Owner:{r[3]} In:{r[4]} Out:{out} Fee:{fee}")


except Error as e:

print(f"View parkings error: {e}")

finally:

[Link]()

def main():

create_database()

create_tables()

while True:

print("\n--- Parking ---")

print("1. Add Spot")

print("2. View Spots")

print("3. Park Vehicle")

print("4. Vehicle Leave")

print("5. Exit")

ch = input("Choice: ").strip()

if ch == "1":

sn = input("Spot number (e.g. P1): ").strip()

if sn:

add_spot(sn)

else:

print("Spot number required.")

elif ch == "2":

view_spots()

elif ch == "3":

try:
sid = int(input("Spot ID to park: ").strip())

veh = input("Vehicle no: ").strip()

owner = input("Owner name: ").strip()

if veh and owner:

park_vehicle(sid, veh, owner)

else:

print("Vehicle no and owner required.")

except ValueError:

print("Invalid Spot ID.")

elif ch == "4":

try:

pid = int(input("Parking ID to close: ").strip())

leave_vehicle(pid)

except ValueError:

print("Invalid Parking ID.")

elif ch == "5":

print("Exit.")

break

else:

print("Invalid choice.")

if __name__ == "__main__":

main()

You might also like