Search code examples
pythonpyqtpymssql

Selecting Table Name Runtime


Is it possible to selecting table name runtime . For example.

def get_data(self, table_name):
    try:
        self._cursor.execute("select Name from %s",(table_name))
        row = self._cursor.fetchall()
        return row
    except:
        raise DbException("An error occured...")

This code is not working .Is there anyway to do this ?


Solution

  • Use format to substitute your placeholder with the table name:

    def get_data(self, table_name):
        try:
            self._cursor.execute("select Name from {}".format(table_name))
            row = self._cursor.fetchall()
            return row
        except:
            raise DbException("An error occured...")