Q:

Dynamic sql query(string template)

Let's say I have a working function that prints first record from  DataBase:
class DataBase:
    def db_firstrecord(self):

        self.execute("SELECT * FROM Flat")
        row = self.fetchall()
        row = row[0]
        print(row)

def main():
    DB.DataBase.db_firstrecord(cur)

I have recently found that there are several ways of making dynamic sql queries(sql string templates). And I want to use my initial function with different table names. How can I implement it in clean and safety manner ? 
I want something like this: 
class DataBase:
    def db_firstrecord(self,`Table_name`):

        self.execute("SELECT * FROM `Table_name`")
        row = self.fetchall()
        row = row[0]
        print(row)

def main():
    DB.DataBase.db_firstrecord(cur,`Table_name`)

A:

Use a whitelist of valid table names.
class Database:
    table_names = {'table1', 'table2', ...}

    def db_firstrecord(self, table_name):
        if table_name not in table_names:
            raise ValueError

        self.execute(f"SELECT * FROM `{table_name}` LIMIT 1")
        row = self.fetch()
        return row

Note that you can't put backticks in around the Python variable in the parameter list. It needs to be in the SQL string.
And there's no reason to fetch all rows if you just want the first. Tell the query to return only one row, and just use fetch() rather than fetchall().

