0

I am trying to add data based on information the user inputs using a Python application. I keep getting errors with my code. The field the view takes inside the database are: dependent_id, first_name, last_name, relationship, and employee_id. The program asks the user for the first, last name, relationship, and employee id. The program should then add the dependent to the database. The dependent ID should increment up by one each time a dependent is added.

def add_dependent(mycursor):
    first_name = input("Enter dependent first name:")
    last_name = input("Enter dependent last name:")
    relationship = input("Enter relationship:")
    employee_id = input("Enter employee ID:")
    dependent_id = "SELECT dependent_id FROM dependents;"
    insert_query = "INSERT INTO dependents(dependent_id, first_name, last_name, relationship, employee_id)" "VALUES (%s,%s, %s, %s, %s);"
    try:
        data = (dependent_id, first_name, last_name, relationship, employee_id)
        mycursor.execute((insert_query, data))
        print(f"\nSuccess: {mycursor.rowcount} dependent added")

        return
    except mysql.connector.Error as error:
        print(f"\nInsert Failed. \n Error: {error.msg}")

I was expecting to be able to insert the data into the database but I am getting the error:

Traceback (most recent call last):
  File "final_project_tjo5fh.py", line 805, in <module>
    main()
  File "final_project_tjo5fh.py", line 787, in main
    add_dependent(mycursor)
  File "final_project_tjo5fh.py", line 725, in add_dependent
    mycursor.execute((insert_query, data))
  File "/usr/local/lib/python3.8/site-packages/mysql/connector/cursor.py", line 528, in execute
    stmt = operation.encode(self._connection.python_charset)
AttributeError: 'tuple' object has no attribute 'encode'
2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.