简体   繁体   中英

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

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

<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. You can only use %s , not %d and %f . So as far as cursor.execute() is concerned, you only provided 4 placeholders for 6 parameters.

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. If args is a dict, %(name)s can be used as a placeholder in the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM