所以我在Flask(Python网站)中使用flask_mysqldb
我正在尝试编写一个函数来删除任何表中的行。 当我对表名进行硬编码时它非常有效,但是我需要做些什么才能使用变量呢?
@app.route('/delete/<string:table>/<string:id>', methods=['POST'])
@is_logged_in
def delete(table, id):
# Create Cursor
cur = mysql.connection.cursor()
# Execute
cur.execute("DELETE FROM %s WHERE id = %s", (table, id))
mysql.connection.commit()
cur.close()
flash('%s deleted'(table), 'success')
return redirect(url_for('dashboard'))
答案 0 :(得分:0)
cur.execute("DELETE FROM %s WHERE id = %s", (table, id))
应该是
cur.execute("DELETE FROM %s WHERE id = %s" % (table, id))