0% found this document useful (0 votes)
4 views6 pages

Parking System CRUD Application

Uploaded by

agloueyome
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)
4 views6 pages

Parking System CRUD Application

Uploaded by

agloueyome
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 tkinter import Tk, Label, Entry, Button, StringVar, messagebox,
Toplevel, ttk

db = [Link](
host="localhost",
user="root",
password="",
database="parking_system_database"
)

cursor = [Link]()

def refresh_parking(tree):
[Link]("SELECT * FROM parking")
records = [Link]()
for record in tree.get_children():
[Link](record)
for record in records:
[Link]("", "end", values=record)

def add_parking(tree, slot_var, type_var, plate_var, brand_var):


slot_number = slot_var.get()
vehicle_type = type_var.get()
plate_number = plate_var.get()
vehicle_brand = brand_var.get()

if not slot_number or not vehicle_type or not plate_number or not


vehicle_brand:
[Link]("Error", "All fields are required!")
return

try:
query = "INSERT INTO gui (slot_number, vehicle_type, plate_number,
vehicle_brand) VALUES (%s, %s, %s, %s)"
[Link](query, (slot_number, vehicle_type, plate_number,
vehicle_brand))
[Link]()
[Link]("Success", "Parking slot added successfully!")
refresh_parking(tree)
except Exception as e:
[Link]("Error", f"Failed to add parking slot: {e}")

def delete_parking(tree):
selected_item = [Link]()
if not selected_item:
[Link]("Error", "No parking slot selected!")
return

try:
item = [Link](selected_item)
parking_id = item["values"][0]
query = "DELETE FROM parking WHERE id = %s"
[Link](query, (parking_id,))
[Link]()
[Link]("Success", "Parking slot deleted
successfully!")
refresh_parking(tree)
except Exception as e:
[Link]("Error", f"Failed to delete parking slot:
{e}")

def update_parking(tree, slot_var, type_var, plate_var, brand_var):


selected_item = [Link]()
if not selected_item:
[Link]("Error", "No parking slot selected!")
return

item = [Link](selected_item)
parking_id = item["values"][0]
slot_number = slot_var.get()
vehicle_type = type_var.get()
plate_number = plate_var.get()
vehicle_brand = brand_var.get()

if not slot_number or not vehicle_type or not plate_number or not


vehicle_brand:
[Link]("Error", "All fields are required!")
return
try:
query = """UPDATE parking
SET slot_number = %s, vehicle_type = %s, plate_number =
%s, vehicle_brand = %s
WHERE id = %s"""
[Link](query, (slot_number, vehicle_type, plate_number,
vehicle_brand, parking_id))
[Link]()
[Link]("Success", "Parking slot updated
successfully!")
refresh_parking(tree)
except Exception as e:
[Link]("Error", f"Failed to update parking slot:
{e}")

def populate_fields(tree, slot_var, type_var, plate_var, brand_var):


selected_item = [Link]()
if not selected_item:
return

item = [Link](selected_item)
values = item["values"]
slot_var.set(values[1])
type_var.set(values[2])
plate_var.set(values[3])
brand_var.set(values[4])

def crud_interface():
root = Tk()
[Link]("Parking System")
[Link](bg="skyblue")

slot_var = StringVar()
type_var = StringVar()
plate_var = StringVar()
brand_var = StringVar()

Label(root, text="Slot Number", bg="skyblue").grid(row=0, column=0,


padx=10, pady=5)
Entry(root, textvariable=slot_var).grid(row=0, column=1, padx=10,
pady=5)

Label(root, text="Vehicle Type", bg="skyblue").grid(row=1, column=0,


padx=10, pady=5)
Entry(root, textvariable=type_var).grid(row=1, column=1, padx=10,
pady=5)

Label(root, text="Plate Number", bg="skyblue").grid(row=2, column=0,


padx=10, pady=5)
Entry(root, textvariable=plate_var).grid(row=2, column=1, padx=10,
pady=5)

Label(root, text="Vehicle Brand", bg="skyblue").grid(row=3, column=0,


padx=10, pady=5)
Entry(root, textvariable=brand_var).grid(row=3, column=1, padx=10,
pady=5)

tree = [Link](root, columns=("ID", "Slot", "Type", "Plate",


"Brand"), show="headings")
[Link]("ID", text="ID")
[Link]("Slot", text="Slot Number")
[Link]("Type", text="Vehicle Type")
[Link]("Plate", text="Plate Number")
[Link]("Brand", text="Vehicle Brand")
[Link](row=4, column=0, columnspan=4, padx=10, pady=10)

refresh_parking(tree)

[Link]("<<TreeviewSelect>>", lambda e: populate_fields(tree,


slot_var, type_var, plate_var, brand_var))

Button(root, text="Add", bg="grey", command=lambda: add_parking(tree,


slot_var, type_var, plate_var, brand_var)).grid(row=5, column=0, padx=10,
pady=5)
Button(root, text="Update", bg="grey", command=lambda:
update_parking(tree, slot_var, type_var, plate_var,
brand_var)).grid(row=5, column=1, padx=10, pady=5)
Button(root, text="Delete", bg="grey", command=lambda:
delete_parking(tree)).grid(row=5, column=2, padx=10, pady=5)
Button(root, text="Refresh", bg="grey", command=lambda:
refresh_parking(tree)).grid(row=5, column=3, padx=10, pady=5)

[Link]()

def login(root, username_var, password_var):


username = username_var.get()
password = password_var.get()
query = "SELECT * FROM users WHERE username = %s AND password = %s"
[Link](query, (username, password))
result = [Link]()
if result:
[Link]("Login Success", "Welcome!")
[Link]()
crud_interface()
else:
[Link]("Error", "Invalid credentials!")

def register():
reg_window = Toplevel()
reg_window.geometry("200x150")
reg_window.title("Register")
reg_window.configure(bg="skyblue")

username_var = StringVar()
password_var = StringVar()

Label(reg_window, text="Username", bg="skyblue").pack()


Entry(reg_window, textvariable=username_var).pack()

Label(reg_window, text="Password", bg="skyblue").pack()


Entry(reg_window, textvariable=password_var, show="*").pack()

def register_user():
username = username_var.get()
password = password_var.get()

if not username or not password:


[Link]("Error", "All fields are required!")
return
try:
query = "INSERT INTO admin (username, password) VALUES (%s,
%s)"
[Link](query, (username, password))
[Link]()
[Link]("Success", "Registration successful!")
reg_window.destroy()
except Exception as e:
[Link]("Error", f"Registration failed: {e}")

Button(reg_window, text="Register", bg="grey",


command=register_user).pack()

def main():
root = Tk()
[Link]("300x200")
[Link]("Login")
[Link](bg="skyblue")

username_var = StringVar()
password_var = StringVar()

Label(root, text="Username", bg="skyblue").pack()


Entry(root, textvariable=username_var).pack()

Label(root, text="Password", bg="skyblue").pack()


Entry(root, textvariable=password_var, show="*").pack()

Button(root, text="Login", bg="grey", command=lambda: login(root,


username_var, password_var)).pack()
Button(root, text="Register", bg="grey", command=register).pack()

[Link]()

if __name__ == "__main__":
main()

Common questions

Powered by AI

The application handles user registrations through a separate interface where users input a username and password. During registration, it checks that no fields are empty. If the inputs are valid, it inserts the new user record into the 'admin' table using a parameterized query to prevent SQL injection, and on success, confirms with a success message; otherwise, it informs the user of the error .

The 'populate_fields' function updates the input fields in the GUI with the details of the selected parking record from the table view. When a user selects a tree item, the function retrieves the item's values and sets the corresponding StringVars for slot number, vehicle type, plate number, and vehicle brand, enabling the user to view and modify existing records .

After completing database operations like add, update, or delete, the system calls `refresh_parking`, which fetches all current database records and repopulates the tree view. This ensures that the data displayed is consistent with the latest database state, providing accurate and up-to-date information to the user .

The document implements try-except blocks for error handling in all database operations involving parking records. For operations like `add_parking`, `update_parking`, and `delete_parking`, if an error occurs during the execution of SQL queries, an error message is displayed with the error details, ensuring the user is informed of the failure and the operations do not terminate abruptly .

The document uses a login system where the user's credentials are checked against the 'users' table in the database. Using the query `SELECT * FROM users WHERE username = %s AND password = %s`, it fetches the record that matches the provided credentials. If a match is found (`result` is true), the CRUD interface is launched; otherwise, an error message is displayed .

To successfully add a new parking slot in the GUI application, the user must first ensure all required fields (slot number, vehicle type, plate number, and vehicle brand) are filled. The 'add_parking' function validates these fields and uses an SQL 'INSERT INTO' command to add the data into the database. If the insertion is successful, the system displays a success message and refreshes the parking display using `refresh_parking` to show the updated list .

When deleting a parking slot, the system first checks if an item is selected in the tree view. If not, it displays an error. If a selection exists, it retrieves the selected item's ID and executes a SQL DELETE command to remove the record from the database. Upon successful deletion, a confirmation message is shown, and the `refresh_parking` function updates the displayed records. Errors during deletion trigger an error message detailing the issue .

The document outlines multiple functionalities for the GUI: adding a parking slot, updating existing parking information, deleting a parking record, and refreshing the table view of parking slots. The GUI also includes features for navigating and manipulating data like buttons to trigger these actions and input fields for user data entry .

The GUI application utilizes parameterized SQL queries during login operations to mitigate SQL injection. By using placeholders %s and passing user inputs as tuple arguments to the `execute` function, it separates user inputs from the SQL logic, thereby preventing malicious input from altering query structure .

The current user authentication method is limited by its reliance on storing passwords in a simple table without obvious hashing or salting techniques, making passwords vulnerable to database breaches. Additionally, the system seems to lack advanced security mechanisms such as multi-factor authentication, and the credentials are checked directly against possibly unencrypted database entries, which could be a security risk .

You might also like