Assign a variable to an SQL statement that assigns the name of the table to which the statement is applied

Viewed 28

I am writing to ask for your help. I am trying to generate a SQL query from a variable, this variable is associated with the table name. The objective of this is that I can choose from a GUI the table I want to access to be able to display the data through the Treeview Widget. I have not managed to generate the SQL statement that allows me to do this.

def run_query(self,query,parameters=()):
    with sqlite.connect(self.db_name) as conn:
        self.cursor=conn.cursor()
        self.result=self.cursor.execute(query,parameters)
        conn.commit()
    return self.result

def view(self,value):#Pasar el parámetro Value desde el SCRIPT PPAL.
    query='SELECT * FROM 'value';'
    parameters=value
    self.run_query(query)
    self.cursor.description()

The variable "value" is comes from another routine associated with the GUI of the application, the idea of ​​this routine is to be able to read the information from the headers of the columns of the selected table through the variable value.

1 Answers

This looks like Python code, but was not specified. What I have used is this:

query = f"""
SELECT {listtable}.id AS {listtable}_id, {listtable}.name AS {listtable}
FROM {listtable}
ORDER BY {listtable} ASC
"""

Where listtable is my variable with the table name.

Related