简体   繁体   English

编程错误:字符串格式化期间的参数数量错误,尝试过元组修复

[英]ProgrammingError: Wrong number of arguments during string formatting, tried tuple fix

so after creating a table, and then calling insert into, I get an error saying wrong number of arguments during string formatting.因此,在创建表,然后调用 insert into 后,我收到一条错误消息,指出在字符串格式化过程中参数数量错误。 I tried looking up the issue, which says make the 2nd paramter a tuple,which i tried to no avail.我尝试查找问题,其中说将第二个参数设为元组,但我尝试无济于事。 Not sure why I'm still getting this error.不知道为什么我仍然收到此错误。 The values for the variables that I'm我是变量的值

Create function:创建函数:

table_ddl = "CREATE TABLE movies (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), year VARCHAR(255), director VARCHAR(255), actor VARCHAR(255), release_date VARCHAR(255), rating VARCHAR(255))"

cnx = ''
try:
    cnx = mysql.connector.connect(user=username, password=password,
                                  host=hostname,
                                  database=db)
except Exception as exp:
    print(exp)
    import MySQLdb
    #try:
    cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db)
    #except Exception as exp1:
    #    print(exp1)

cur = cnx.cursor()

try:
    cur.execute(table_ddl)
    cnx.commit()
    populate_data()
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
        print("already exists.")
    else:
        print(err.msg)

Insert function: title,year, and etc take from html requests插入函数:标题、年份等取自 html 请求

def add_to_db():
    print("Received request.")
    title = request.form['title']
    year = request.form['year']
    director = request.form['director']
    actor = request.form['actor']
    release_date = request.form['release_date']
    rating = request.form['rating']
    db, username, password, hostname = get_db_creds()

    cnx = ''
    try:
        cnx = mysql.connector.connect(user=username, password=password,
                                      host=hostname,
                                      database=db)
    except Exception as exp:
        print(exp)
        import MySQLdb
        cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db)

    cur = cnx.cursor()
    sql = "INSERT INTO movies (title,year,director,actor,release_date,rating) VALUES (%s,%d,%s,%s,%s,%f)"
    val = [title,year,actor,director,release_date,rating]
    cur.execute(sql,val) # line with error
    cnx.commit()
    return hello()

HTML Code HTML代码

<form action="add_to_db" method="post">
  <h4>Insert Movie</h4>
  <br>
  Year: <input type="text" name="year"><br>
  Title: <input type="text" name="title"><br>
  Director: <input type="text" name="director"><br>
  Actor: <input type="text" name="actor"><br>
  Release Date: <input type="text" name="release_date"><br>
  Rating: <input type="text" name="rating"><br>
  <input type="submit" value="Insert Movie">
</form>

The placeholders used by cursor.execute() are not general purpose % formatting operators. cursor.execute()使用的占位符不是通用的%格式化操作符。 You can only use %s , not %d and %f .您只能使用%s ,不能使用%d%f So as far as cursor.execute() is concerned, you only provided 4 placeholders for 6 parameters.因此,就cursor.execute()而言,您只为 6 个参数提供了 4 个占位符。

So it should be:所以应该是:

sql = "INSERT INTO movies (title,year,director,actor,release_date,rating) VALUES (%s,%s,%s,%s,%s,%s)"

From the documentation :文档

If args is a list or tuple, %s can be used as a placeholder in the query.如果args是列表或元组,则%s可用作查询中的占位符。 If args is a dict, %(name)s can be used as a placeholder in the query.如果args是一个 dict, %(name)s可以用作查询中的占位符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ProgrammingError:字符串格式化过程中的参数数量错误 - ProgrammingError: Wrong number of arguments during string formatting mysql.connector.errors.ProgrammingError:字符串格式化期间参数数量错误 - mysql.connector.errors.ProgrammingError: Wrong number of arguments during string formatting ProgrammingError:并非所有参数都在字符串格式化期间转换 - ProgrammingError: not all arguments converted during string formatting flask-mysql编程错误:字符串格式化期间并非所有参数都已转换 - flask-mysql ProgrammingError: not all arguments converted during string formatting TypeError:在字符串格式化期间并非所有参数都转换为元组python中的错误 - TypeError: not all arguments converted during string formatting Error in tuple python 无法将Scrapy项目用作MySQL.execute的数据:“字符串格式化期间参数数量错误” - Unable to use Scrapy item as data for MySQL.execute: “Wrong number of arguments during string formatting” 如何修复并非所有在字符串格式化期间转换的参数? - How can I fix not all arguments converted during string formatting? 如何修复 TypeError:并非所有参数都在字符串格式化期间转换 - How to fix TypeError: not all arguments converted during string formatting 如何修复 Python 中的“TypeError:并非所有参数都在字符串格式化期间转换” - How to fix 'TypeError: not all arguments converted during string formatting' in Python 字符串格式的参数个数 - Number of Arguments for String Formatting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM