[英]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
可用作查询中的占位符。 Ifargs
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.