Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 编程错误:字符串格式化期间参数数量错误,已尝试元组修复_Python_Html_Mysql - Fatal编程技术网

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

Python 编程错误:字符串格式化期间参数数量错误,已尝试元组修复,python,html,mysql,Python,Html,Mysql,因此,在创建了一个表,然后调用insert into之后,我在字符串格式化过程中遇到了一个错误,即参数数量错误。我试着查找这个问题,它说让第二个参数成为一个元组,我试着没有用。不知道为什么我还是会犯这个错误。我正在处理的变量的值 创建函数: table_ddl = "CREATE TABLE movies (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), year VARCHAR(255), director VARCHAR(255)

因此,在创建了一个表,然后调用insert into之后,我在字符串格式化过程中遇到了一个错误,即参数数量错误。我试着查找这个问题,它说让第二个参数成为一个元组,我试着没有用。不知道为什么我还是会犯这个错误。我正在处理的变量的值

创建函数:

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)
插入函数:标题、年份等取自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代码

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

插入电影

年份:
标题:
导演:
演员:
发布日期:
评级:

游标.execute()使用的占位符不是通用的
%
格式运算符。您只能使用
%s
,不能使用
%d
%f
。就
cursor.execute()
而言,您只为6个参数提供了4个占位符

因此,它应该是:

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

如果
args
是列表或元组,
%s
可以用作查询中的占位符。如果
args
是一个dict,
%(name)s
可以用作查询中的占位符


不,我把它作为元组修复的一部分放在那里,即使没有它也不起作用,我试着在每个元组中都放逗号,这不是元组修复。您考虑的是
(变量,)
,但这是Python,而不是SQL。如果您谈论的是“val=[标题、年份、演员、导演、发行日期、评级]”,请注意使用
,除了类似的例外情况
,请参见。