flask_mysqldb Delete FROM variable table [duplicate]

2024/7/7 5:59:40

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 Cursorcur = mysql.connection.cursor()# Executecur.execute("DELETE FROM %s WHERE id = %s", (table, id))mysql.connection.commit()cur.close()flash('%s deleted'(table), 'success')return redirect(url_for('dashboard'))
Answer
cur.execute("DELETE FROM %s WHERE id = %s", (table, id))

should be

cur.execute("DELETE FROM %s WHERE id = %s" % (table, id))
https://en.xdnf.cn/q/119646.html

Related Q&A

Syntax Error at the end of a while loop

EDIT: This question was ask at the start of my learning process for python. The Syntax Error was produced by pythons IDLE with no trackback to speak of. This was the main cause of the problem and confu…

Creating an adjacency list class in Python

I was wondering how to create an adjacency list class Here is what I have so far:class AdjNode:def __init__(self, value):self.vertex = valueself.next = Noneclass Graph:def __init__(self):# Add edgesdef…

How can I separate a rust library and the pyo3 exported python extensions which wrap it

I have a rust library which provides useful functionality for use in other rust programs. Additionally I would like to provide this functionality as a python extension (using pyo3 and setuptools-rust, …

how do I count unique words of text files in specific directory with Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python convert path to dict

I have a list of paths that need to be converted to a dict ["/company/accounts/account1/accountId=11111","/company/accounts/account1/accountName=testacc","/company/accounts/acc…

Python: How to download images with the URLs in the excel and replace the URLs with the pictures?

As shown in the below picture,theres an excel sheet and about 2,000 URLs of cover images in the F column. What I want to do is that downloading the pictures with the URLs and replace the URL with the…

I cant figure out pip tensorrt line 17 error

I couldnt install it in any way, I wonder what could be the cause of the error. I installed C++ and other necessary stuff I am using windows 11 I installed pip install nvidia-pyindex with no problem. S…

Extracting specific values for a header in different lines using regex

I have text string which has multiple lines and each line has mix of characters/numbers and spaces etc. Here is how a couple lines look like:WEIGHT VOLUME CHA…

Creating a function to process through a .txt file of student grades

Can someone help...My driver file is here:from functions import process_marks def main():try:f = open(argv[1])except FileNotFoundError:print("\nFile ", argv[1], "is not available")e…

Python Reddit PRAW get top week. How to change limit?

I have been familiarising myself with PRAW for reddit. I am trying to get the top x posts for the week, however I am having trouble changing the limit for the "top" method. The documentatio…