Angular,Flask,mysql.connector编程错误:参数数量错误

时间:2016-12-03 18:55:48

标签: python mysql angularjs flask

我使用Angular,Flask和MySQL.connector连接到MySQL数据库:

这是我的python烧瓶代码处理帖子请求插入新的"电影":

@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

我知道它不是我的Angularjs,因为我的浏览器控制台显示内部服务器错误(500)所以我开始打印出由flask和mysql.connector处理的insert语句:

('INSERT INTO Movie (idMovie, MovieName, MovieYear) VALUES (%d, %s, %d)', (100, 'Test', 2010))

这似乎是正确的。

然而我一直在

    "Wrong number of arguments during string formatting")
ProgrammingError: Wrong number of arguments during string formatting

=============================================== ================================

感谢答案,它是固定的,对于那些想知道这是我将代码切换到:

的人
@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

2 个答案:

答案 0 :(得分:1)

SQL参数替换与字符串格式不同。您应该始终使用 @Test public void points_shouldCreatInstance() { //Create Point object (calls Point class constructor) Point point = new Point(1.0f,2.0f); //Check x is set inside the created Point object assertEquals(1.0f, point.get_x()); //Check y is set inside the created Point object assertEquals(2.0f, point.get_y()); } ,即使是整数。

答案 1 :(得分:1)

检查the docs,它说cursor.execute()方法根据需要将事物转换为数据库理解的东西。看来你应该只在字符串中使用zonedDateTime.format(formatter) 占位符,并在execute方法中使用其他所有东西。