0

I'm getting the exception _mysql_exceptions.ProgrammingError: not all arguments converted during string formatting when calling this function to insert a student. The number of parameters in the query matches the number of arguments I'm passing though. My other SQL queries work fine. I am using Flask-MySQLdb on Python 3 to handle the MySQL connection.

def create_student(surname, forename, dob, address, phone, gender, tutor, email):
    cursor = mysql.connection.cursor()
    cursor.execute('''
        INSERT INTO students(surname, forename, dob, address, phone, gender, tutor, email) 
        VALUES(?, ?, ?, ?, ?, ?, ?, ?)''', (surname, forename, dob, address, phone, gender, tutor, email))
    mysql.connection.commit()
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/mnt/c/Users/root/Desktop/student-admin/main.py", line 184, in create
    if create_student(surname, forename, dob, address, phone, gender, tutor, email):
  File "/mnt/c/Users/root/Desktop/student-admin/main.py", line 60, in create_student
    VALUES(?, ?, ?, ?, ?, ?, ?, ?)''', (surname, forename, dob, address, phone, gender, tutor, email))
  File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py", line 240, in execute
    self.errorhandler(self, ProgrammingError, str(m))
  File "/usr/local/lib/python3.5/dist-packages/MySQLdb/connections.py", line 52, in defaulterrorhandler
    raise errorclass(errorvalue)
_mysql_exceptions.ProgrammingError: not all arguments converted during string formatting
0

1 Answer 1

3

The correct way of writing prepared statements is the following:

def create_student(surname, forename, dob, address, phone, gender, tutor, email):
    cursor = mysql.connection.cursor()
    cursor.execute('''
        INSERT INTO students(surname, forename, dob, address, phone, gender, tutor, email)
        VALUES(%s, %s, %s, %s, %s, %s, %s, %s)''', (surname, forename, dob, address, phone, gender, tutor, email))
    mysql.connection.commit()

The error comes from the fact that the mysql module does not find where to put the paramters you are giving it, because it does not interpret the questions marks as placeholders, and thus is producing an error telling you that _mysql_exceptions.ProgrammingError: not all arguments converted during string formatting, which in human language, means it could not fit your aguments in the format string.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.