import mysql.connector
# Establish database connection
cnx = mysql.connector.connect(user='your_username', password='your_password',
host='your_host', database='your_database')
# Create a cursor object to execute queries
cursor = cnx.cursor()
# Example insert query where 'name' field is provided
insert_query = "INSERT INTO your_table (name, other_field) VALUES (%s, %s)"
# Example data to be inserted
record = ("John Doe", "some value")
try:
# Execute the query
cursor.execute(insert_query, record)
# Commit the changes to the database
cnx.commit()
# Output success message
print("Record inserted successfully!")
except mysql.connector.Error as error:
# Output the error message
print(f"Error inserting record: {error}")
# Close the cursor and connection
cursor.close()
cnx.close()