问题描述
我正在尝试从@R_995_4045@ION_SCHEMA.COLUMNS导出table_schema,table_name,column_name,并将其导出到JSON文件中。我成功地做到了。我需要使用JSON文件在HTML中显示项目。
我的数据库的EG:
Table_Schema: has 5 different types of schema
Table_name: Each schema has about 4-5 table
Column_Name: Each table has about 4 columns name
这是我导出的JSON文件的示例
[
{
"table_schema": "A","table_name": "TA","column_name": "typname",},{
"table_schema": "A","table_name": "TB","column_name": "typnamespace","table_name": "TC","column_name": "typowner",{
"table_schema": "B","table_name": "FB","column_name": "typlen","table_name": "FG","column_name": "typbyval",{
"table_schema": "C","table_name": "CQ","column_name": "typtype","column_name": "typisdefined","table_name": "CO","column_name": "typdelim",} .......20339 ROWS MORE
Python文件
GetAllTables():
conn = None
try:
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute("""SELECT TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME
FROM @R_995_4045@ION_SCHEMA.COLUMNS;""")
rows = cur.fetchall()
rowarray_list = []
for row in rows:
rowarray_list.append({'table_schema':row[0],'table_name': row[1],'column_name':row[2]
})
rowarray_file= 'tables1.json'
with open(rowarray_file,'w') as outfile:
j = json.dump(rowarray_list,outfile,indent=4)
cur.close()
except(Exception,psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
因此,当我尝试在HTML中显示数据时,将重复打印出Table_Schema和Table_Name。
我尝试使用_.uniqBy函数,但是它消除了列表中的其他对象
_。uniqBy():
var someObject = require('../table_schema_name.json')
var uniqueALR = uniqBy(someObject,'table_schema');
结果:
[
{ table_schema: 'A',table_name: 'TA',column_name : 'typname'},{
table_schema: 'B',table_name: 'FB',column_name : 'typlen'
},{
table_schema: 'C',table_name: 'CQ',column_name : 'typtype'
},
是否有更好的方法可以只显示一个不同的table_schema和table_name而不删除其他重要数据?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)