-1

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
  • Your code snippet seems to use a variable for the table name. Are you getting an error when executing the delete function? Commented Dec 29, 2017 at 15:41

1 Answer 1

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

should be

cur.execute("DELETE FROM %s WHERE id = %s" % (table, id))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.