是否可以选择表名运行时。例如。
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...")
此代码无效。无论如何都要这样做吗?
答案 0 :(得分:1)
使用format
将占位符替换为表名:
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...")