我写了一篇网页.pypython中的服务来访问PostGres并获取特定数据库中的表名。在
代码:
def GET(self,r):
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
tables = []
datasetID = web.input().dataSetID
cursor = conn.cursor()
cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
tablesWithDetails = cursor.fetchall()
print tablesWithDetails
for x in tablesWithDetails:
x.replace("(", "")
x.replace(")","")
tables.append(x)
print tables;
这将打印如下表:
^{pr2}$所需输出:
['acam_datasegregationdetails', 'acam_datasegregationheader', idn_accessinformation', 'idn_b2cuseraccountmapping', 'idn_b2cuserdevicemapping', 'idn_b2cusers', 'idn_roles', 'idn_useraccountmapping']
扔掉那个循环,然后进去
它将构建一个包含结果集中每个元组的第一个元素的列表。在
或者更简单(也更便宜),如果您想要一个列表,那么返回一个数组,它将由Psycopg调整为一个列表:
^{pr2}$问题出在这段代码上
当您执行
cursor.fetchall()
时,您将得到一个元组列表当您执行
for x in tablesWithDetails:
操作时,您正在按one tuple at a time
对列表进行迭代所以当你做
tables.append(x)
时,你就是在列表中附加一个元素元组为了改变您可以这样做
tables.append(x[0])
,它附加了元组的第一个元素相关问题 更多 >
编程相关推荐