0% found this document useful (0 votes)
7 views

import sqlite3a

ddd dd

Uploaded by

dodoisextinct7
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

import sqlite3a

ddd dd

Uploaded by

dodoisextinct7
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import sqlite3

from datetime import datetime

# Connect to SQLite database (it will create a new file if it doesn't exist)
conn = sqlite3.connect('parking_lot.db')
cursor = conn.cursor()

# Create tables for vehicles and parking records


def create_tables()
cursor.execute('''
CREATE TABLE IF NOT EXISTS vehicles (
vehicle_id INTEGER PRIMARY KEY AUTOINCREMENT,
license_plate TEXT NOT NULL,
vehicle_type TEXT,
owner_name TEXT
)
''')

cursor.execute('''
CREATE TABLE IF NOT EXISTS parking_slots (
slot_id INTEGER PRIMARY KEY AUTOINCREMENT,
status TEXT DEFAULT 'available'
)
''')

cursor.execute('''
CREATE TABLE IF NOT EXISTS parking_records (
record_id INTEGER PRIMARY KEY AUTOINCREMENT,
vehicle_id INTEGER,
slot_id INTEGER,
entry_time TEXT,
exit_time TEXT,
FOREIGN KEY(vehicle_id) REFERENCES vehicles(vehicle_id),
FOREIGN KEY(slot_id) REFERENCES parking_slots(slot_id)
)
''')
conn.commit()

# Function to add a new vehicle to the database


def add_vehicle(license_plate, vehicle_type, owner_name)
cursor.execute('''
INSERT INTO vehicles (license_plate, vehicle_type, owner_name)
VALUES (, , )
''', (license_plate, vehicle_type, owner_name))
conn.commit()

# Function to add a parking slot (initially set to 'available')


def add_parking_slot()
cursor.execute('''
INSERT INTO parking_slots (status)
VALUES ('available')
''')
conn.commit()

# Function to record a vehicle entry (check-in)


def vehicle_entry(license_plate)
cursor.execute('''
SELECT vehicle_id FROM vehicles WHERE license_plate =
''', (license_plate,))
vehicle = cursor.fetchone()

if vehicle
vehicle_id = vehicle[0]
cursor.execute('''
SELECT slot_id FROM parking_slots WHERE status = 'available' LIMIT 1
''')
slot = cursor.fetchone()

if slot
slot_id = slot[0]
entry_time = datetime.now().strftime('%Y-%m-%d %H%M%S')
cursor.execute('''
INSERT INTO parking_records (vehicle_id, slot_id, entry_time)
VALUES (, , )
''', (vehicle_id, slot_id, entry_time))

# Mark the parking slot as occupied


cursor.execute('''
UPDATE parking_slots SET status = 'occupied' WHERE slot_id =
''', (slot_id,))
conn.commit()
print(fVehicle {license_plate} entered at {entry_time} into slot
{slot_id}.)
else
print(No available parking slots.)
else
print(Vehicle not found.)

# Function to record vehicle exit (check-out)


def vehicle_exit(license_plate)
cursor.execute('''
SELECT vehicle_id FROM vehicles WHERE license_plate =
''', (license_plate,))
vehicle = cursor.fetchone()

if vehicle
vehicle_id = vehicle[0]
cursor.execute('''
SELECT FROM parking_records WHERE vehicle_id = AND exit_time IS NULL
ORDER BY entry_time DESC LIMIT 1
''', (vehicle_id,))
record = cursor.fetchone()

if record
record_id = record[0]
exit_time = datetime.now().strftime('%Y-%m-%d %H%M%S')
cursor.execute('''
UPDATE parking_records SET exit_time = WHERE record_id =
''', (exit_time, record_id))

# Free up the parking slot


slot_id = record[2]
cursor.execute('''
UPDATE parking_slots SET status = 'available' WHERE slot_id =
''', (slot_id,))
conn.commit()
print(fVehicle {license_plate} exited at {exit_time} from slot
{slot_id}.)
else
print(This vehicle has no ongoing parking record.)
else
print(Vehicle not found.)

# Example usage
def main()
create_tables() # Set up the database and tables

# Add vehicles and parking slots (this can be done once)


add_vehicle(ABC123, Car, John Doe)
add_vehicle(XYZ456, Truck, Jane Doe)

# Add parking slots (this can be done once too)


add_parking_slot()
add_parking_slot()

# Vehicle entry
vehicle_entry(ABC123) # Vehicle ABC123 enters the parking lot

# Vehicle exit
vehicle_exit(ABC123) # Vehicle ABC123 exits the parking lot

# You can continue adding more vehicles and records as needed.

if __name__ == __main__
main()
conn.close()

You might also like