Import Mysq1123

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

import mysql.

connector

def get_int_input(prompt):

while True:

try:

return int(input(prompt))

except ValueError:

print("Invalid input. Please enter a valid number.")

def read_file(filename):

if os.path.exists(filename):

with open(filename, 'r') as f:

data = f.read()

print(data)

else:

print(f"File {filename} does not exist.")

def connect_to_database():

try:

# Establishing the MySQL connection

con = mysql.connector.connect(

host='localhost',

password='jay8811',

user='root',

database='store'
)

if con.is_connected():

print('Successfully connected')

c = con.cursor()

print('Grocery Shop Management System')

print('1. Login')

print('2. Exit')

choice = get_int_input('Enter your choice: ')

if choice == 1:

user_name = input('Enter your username: ')

password = input('Enter your password: ')

# Check for valid credentials

if user_name == 'jay' and password == 'jay123':

print('Connected successfully')

while True:

print('\nGrocery Shop Menu')

print('1. Customer Details')

print('2. Product Details')

print('3. Worker Details')

print('4. See All Customer Details')

print('5. See All Product Details')

print('6. See All Worker Details')


print('7. See One Customer Details')

print('8. See One Product Details')

print('9. See One Worker Details')

print('10. Stocks')

print('11. Pie Chart for Availability of Stocks')

choice = get_int_input('Enter your choice: ')

if choice == 1:

# Insert customer details

cust_name = input('Enter customer name: ')

phone_no = get_int_input('Enter phone number: ')

cost = float(input('Enter the cost: '))

insert_customer_details(c, phone_no, cust_name, cost)

elif choice == 2:

# Insert product details

product_name = input('Enter product name: ')

product_cost = float(input('Enter product cost: '))

insert_product_details(c, product_name, product_cost)

elif choice == 3:

# Insert worker details

worker_name = input('Enter worker name: ')

worker_role = input('Enter worker role: ')


worker_age = get_int_input('Enter worker age: ')

worker_salary = float(input('Enter worker salary: '))

phone_no = get_int_input('Enter phone number: ')

insert_worker_details(c, worker_name, worker_role, worker_age, worker_salary,


phone_no)

elif choice == 4:

# Display all customer details

display_all_customer_details(c)

elif choice == 5:

# Display all product details

display_all_product_details(c)

elif choice == 6:

# Display all worker details

display_all_worker_details(c)

elif choice == 7:

# Display one customer detail

name = input('Enter customer name: ')

display_one_customer_detail(c, name)

elif choice == 8:

# Display one product detail

name = input('Enter product name: ')


display_one_product_detail(c, name)

elif choice == 9:

# Display one worker detail

name = input('Enter worker name: ')

display_one_worker_detail(c, name)

elif choice == 10:

# Show stocks from a file

print('**************')

read_file('test.txt')

print('**************')

elif choice == 11:

# Pie chart for item availability

display_stock_pie_chart()

elif choice == 2:

print("Exiting...")

break # Exit the loop cleanly

else:

print("Invalid choice, please try again.")

else:
print("Invalid username or password.")

elif choice == 2:

print("Exiting...")

return # Exit the function cleanly

except mysql.connector.Error as err:

print(f"Error: {err}")

finally:

if con.is_connected():

con.close()

print("Database connection closed.")

def insert_customer_details(c, phone_no, cust_name, cost):

sql_insert = "INSERT INTO customer_details (phone_no, cust_name, cost) VALUES (%s, %s, %s)"

c.execute(sql_insert, (phone_no, cust_name, cost))

c.connection.commit()

print('Customer data is updated')

def insert_product_details(c, product_name, product_cost):

sql_insert = "INSERT INTO product_details (product_name, product_cost) VALUES (%s, %s)"

c.execute(sql_insert, (product_name, product_cost))

c.connection.commit()

print('Product data is updated')


def insert_worker_details(c, worker_name, worker_role, worker_age, worker_salary, phone_no):

sql_insert = "INSERT INTO worker_details (worker_name, worker_role, worker_age, worker_salary,


phone_no) VALUES (%s, %s, %s, %s, %s)"

c.execute(sql_insert, (worker_name, worker_role, worker_age, worker_salary, phone_no))

c.connection.commit()

print('Worker data is updated')

def display_all_customer_details(c):

c.execute('SELECT phone_no, cust_name, cost FROM customer_details')

records = c.fetchall()

for record in records:

print(record)

def display_all_product_details(c):

c.execute('SELECT product_name, product_cost FROM product_details')

records = c.fetchall()

for record in records:

print(record)

def display_all_worker_details(c):

c.execute('SELECT worker_name, worker_role, worker_age, worker_salary FROM worker_details')

records = c.fetchall()

for record in records:

print(record)

def display_one_customer_detail(c, name):


c.execute('SELECT phone_no, cust_name, cost FROM customer_details WHERE cust_name = %s',
(name,))

records = c.fetchall()

for record in records:

print(record)

def display_one_product_detail(c, name):

c.execute('SELECT product_name, product_cost FROM product_details WHERE product_name = %s',


(name,))

records = c.fetchall()

for record in records:

print(record)

def display_one_worker_detail(c, name):

c.execute('SELECT worker_name, worker_role, worker_age, worker_salary FROM worker_details


WHERE worker_name = %s', (name,))

records = c.fetchall()

for record in records:

print(record)

def display_stock_pie_chart():

items = ['Shoes', 'Stationary', 'Watch', 'House Use', 'Food Items']

availability = [156, 200, 103, 206, 196]

colours = ['red', 'yellowgreen', 'blue', 'gold', 'lightcoral']

plt.pie(availability, labels=items, colors=colours)

plt.title('Availability of Items in Shop')

plt.show()
# Entry point for the application

if '_name_' == '_main_':

connect_to_database()

You might also like