0% found this document useful (0 votes)
13 views2 pages

MySQL Staff Record Insertion Script

The document is a Python script that accepts user input for staff details including ID, name, date of joining, and salary. It connects to a MySQL database to insert this information into a 'STAFF' table. The script includes error handling for date formatting and database connection issues.

Uploaded by

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

MySQL Staff Record Insertion Script

The document is a Python script that accepts user input for staff details including ID, name, date of joining, and salary. It connects to a MySQL database to insert this information into a 'STAFF' table. The script includes error handling for date formatting and database connection issues.

Uploaded by

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

import mysql.

connector

from datetime import datetime

# Accepting input from the user

sid = int(input("Enter Staff ID: "))

sname = input("Enter Staff Name: ")

doj_input = input("Enter Date of Joining (YYYY-MM-DD): ")

salary = float(input("Enter Salary: "))

# Convert string date to proper format

try:

doj = [Link](doj_input, "%Y-%m-%d").date()

except ValueError:

print("Invalid date format. Please use YYYY-MM-DD.")

exit()

# Establish connection to MySQL

try:

conn = [Link](

host="localhost",

user="root",

password="tiger",

database="COMPANY"

cursor = [Link]()

# Prepare SQL query

query = "INSERT INTO STAFF (SID, SNAME, DOJ, SALARY) VALUES (%s, %s, %s, %s)"

values = (sid, sname, doj, salary)

# Execute and commit


[Link](query, values)

[Link]()

print("Record inserted successfully!")

except [Link] as err:

print("Error:", err)

finally:

if conn.is_connected():

[Link]()

[Link]()

You might also like