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

前端 未结 1 1184
不知归路
不知归路 2020-12-22 11:42

I\'m getting the exception _mysql_exceptions.ProgrammingError: not all arguments converted during string formatting when calling this function to insert a stude

相关标签:
1条回答
  • 2020-12-22 12:16

    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 讨论(0)
提交回复
热议问题