0% found this document useful (0 votes)
14 views3 pages

CS3

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)
14 views3 pages

CS3

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
You are on page 1/ 3

Automobile Service Station

CREATE TABLE services (


id INT AUTO_INCREMENT PRIMARY KEY,
vehicle_id VARCHAR(255) NOT NULL,
service_type VARCHAR(255) NOT NULL,
cost DECIMAL(10, 2) NOT NULL
);

import mysql.connector
from mysql.connector import Error

# Constants
DATABASE_NAME = 'service_station'
LOG_FILE = 'service_log.txt'

# Function to connect to MySQL database


def create_connection():
connection = None
try:
connection = mysql.connector.connect(
host='localhost',
user='your_username', # replace with your MySQL username
password='your_password', # replace with your MySQL password
database=DATABASE_NAME
)
except Error as e:
print(f"Error: '{e}'")
return connection

# Log operations to a text file


def log_operation(operation):
with open(LOG_FILE, 'a') as log_file:
log_file.write(f"{operation}\n")

# Add a new service record


def add_service(vehicle_id, service_type, cost):
connection = create_connection()
cursor = connection.cursor()
try:
cursor.execute('INSERT INTO services (vehicle_id, service_type, cost) VALUES (%s, %s, %s)',
(vehicle_id, service_type, cost))
connection.commit()
log_operation(f"Added service for Vehicle ID: {vehicle_id}, Service: {service_type}, Cost: {cost}")
except Error as e:
print(f"An error occurred: {e}")
finally:
cursor.close()
connection.close()
# View all service records
def view_services():
connection = create_connection()
cursor = connection.cursor()
try:
cursor.execute('SELECT * FROM services')
services = cursor.fetchall()
for service in services:
print(f"ID: {service[0]}, Vehicle ID: {service[1]}, Service Type: {service[2]}, Cost: {service[3]}")
except Error as e:
print(f"An error occurred: {e}")
finally:
cursor.close()
connection.close()

# Delete a service record


def delete_service(service_id):
connection = create_connection()
cursor = connection.cursor()
try:
cursor.execute('DELETE FROM services WHERE id = %s', (service_id,))
connection.commit()
log_operation(f"Deleted service with ID: {service_id}")
except Error as e:
print(f"An error occurred: {e}")
finally:
cursor.close()
connection.close()

# Main program loop


def main():
while True:
print("\nAutomobile Service Station")
print("1. Add Service Record")
print("2. View Service Records")
print("3. Delete Service Record")
print("4. Exit")

choice = input("Choose an option (1-4): ")

if choice == '1':
vehicle_id = input("Enter Vehicle ID: ")
service_type = input("Enter Service Type: ")
cost = float(input("Enter Service Cost: "))
add_service(vehicle_id, service_type, cost)
elif choice == '2':
view_services()
elif choice == '3':
service_id = input("Enter service ID to delete: ")
delete_service(service_id)
elif choice == '4':
print("Exiting program.")
break
else:
print("Invalid choice, please try again.")

if __name__ == '__main__':
main()

You might also like