选择 Table 名称运行时

Selecting Table Name Runtime

是否可以选择 table name runtime 。例如。

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...")

此代码无效。有没有办法做到这一点?

使用 format 将您的占位符替换为 table 名称:

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...")