Number of MySQL query parameters match arguments passed to execute, but Python raises “not all arguments converted”

╄→гoц情女王★ 提交于 2019-12-18 09:51:01

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/45110251/number-of-mysql-query-parameters-match-arguments-passed-to-execute-but-python-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!