一尘不染

MySQL查询参数的数量与传递给执行的参数相匹配,但是Python提出“所有已转换的参数”。

sql

_mysql_exceptions.ProgrammingError: not all arguments converted during string formatting调用此函数以插入学生时出现异常。查询中的参数数量与我通过的参数数量相匹配。我的其他SQL查询工作正常。我在Python
3上使用Flask-MySQLdb处理MySQL连接。

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

阅读 135

收藏
2021-03-17

共1个答案

一尘不染

编写预备语句的正确方法如下:

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()

该错误来自以下事实:mysql模块找不到要放置的参数放在哪里,因为它没有将问号解释为占位符,因此产生了一个错误,告诉您_mysql_exceptions.ProgrammingError: not all arguments converted during string formatting,这在人类语言中意味着它不适合格式字符串中的参数。

2021-03-17