flask_mysqldb Delete FROM variable table [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-13 22:08:50

问题


So i use flask_mysqldb in a Flask(Python website)

I'm trying to write a function to delete a row in any table. It works perfectly when I hardcode the tablename, but what do I need to do to be able to use a variable instead?

@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'))

回答1:


cur.execute("DELETE FROM %s WHERE id = %s", (table, id))

should be

cur.execute("DELETE FROM %s WHERE id = %s" % (table, id))


来源:https://stackoverflow.com/questions/48024806/flask-mysqldb-delete-from-variable-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!