[英]Angular, Flask, mysql.connector ProgrammingError: Wrong number of arguments
I'm using Angular, Flask and MySQL.connector to connect to a MySQL database: 我正在使用Angular,Flask和MySQL.connector连接到MySQL数据库:
This is my python flask code handling post requests inserting a new "movie": 这是我的python flask代码处理后请求,要求插入新的“电影”:
@app.route("/addMovies", methods=['POST'])
def addMovies():
cnx = mysql.connector.connect(user='root', database='MovieTheatre')
cursor = cnx.cursor()
insert_stmt = (
"INSERT INTO Movie (idMovie, MovieName, MovieYear) "
"VALUES (%d, %s, %d)"
)
post = request.get_json()
#data = (post['idMovie'], post['MovieName'], post['MovieYear'])
data = (100, 'Test', 2010) # test data
print(insert_stmt,data)
cursor.execute(insert_stmt,data)
cnx.commit()
cnx.close()
return data
I know its not my Angularjs, because my browser console says Internal Server Error (500) so I started printing out the insert statement handled by flask and mysql.connector: 我知道它不是我的Angularjs,因为我的浏览器控制台显示“内部服务器错误(500)”,所以我开始打印出flask和mysql.connector处理的插入语句:
('INSERT INTO Movie (idMovie, MovieName, MovieYear) VALUES (%d, %s, %d)', (100, 'Test', 2010))
Which seems correct. 这似乎是正确的。
However I keep getting 但是我不断
"Wrong number of arguments during string formatting")
ProgrammingError: Wrong number of arguments during string formatting
=============================================================================== ================================================== ============================
Thanks to the answers, its fixed, for those wondering this is what I switched my code to : 感谢那些固定的答案,对于那些想知道这是我将代码切换到以下内容的人来说:
@app.route("/addMovies", methods=['POST'])
def addMovies():
cnx = mysql.connector.connect(user='root', database='MovieTheatre')
cursor = cnx.cursor()
insert_stmt = (
"INSERT INTO Movie (idMovie, MovieName, MovieYear) "
"VALUES (%s, %s, %s)"
)
post = request.get_json()
data = (post['idMovie'], post['MovieName'], post['MovieYear'])
print(insert_stmt,data)
cursor.execute(insert_stmt,data)
cnx.commit()
cnx.close()
return data
SQL parameter substitution is not the same as string formatting. SQL参数替换与字符串格式不同。 You should always use
%s
, even for integers. 您应该始终使用
%s
,即使是整数也是如此。
Check the docs , it says the cursor.execute() method converts things as necessary to something the database understands. 检查文档 ,它说cursor.execute()方法将必要的内容转换为数据库可以理解的内容。 It seems you are supposed to use only
%s
placeholders in your string and let everything else on the execute method. 看来您应该只在字符串中使用
%s
占位符,并将其他所有内容都放在execute方法上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.