0
我使用psycopg2和Python來查詢PostGIS數據庫中的表。這是我建,以排除該列的查詢:the_geom通過psycopg2創建連接查詢
sqlstr = "SELECT 'SELECT ' || ARRAY_TO_STRING(ARRAY(SELECT COLUMN_NAME::VARCHAR(50) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='{table}' AND COLUMN_NAME NOT IN ('the_geom') ORDER BY ORDINAL_POSITION), ', ') || ' FROM {table}';".format(** {
'table': name
})
cur.execute(sqlstr)
sqlstr = cur.fetchall()
print(sqlstr)
它返回這樣的:
SELECT fid, FS2011, NAME_1, Admin1, ANUAK, SIDAMO, BAGIRMI, BAKOMO, KOMA, KARAMOJO, SOUTHERN_L, DAGO, DINKA, ACHOLI, MURLE, LOTUKO, Area_m2, BARI_pc, SUDARAB_pc, SEREMUN_pc, AZANDE_pc, BANDA_pc, MORUMANGpc, NUER_pc, ANUAK_pc, TES_pc, SIDAM_pc, BERTA_pc, NORTH_L_pc, BAGRIMI_pc, BAKOMO_pc, KOMA_pc, KARAMOJOpc, SOUTJ_L_pc, DAGO_pc, DINKA_pc, ACHOLI_pc, MURLE_pc, LOTUK_pc, MAJORITY FROM ssd_ethnicitystatstics_adm1'
的問題是列名,他們本來是資本。出於這個原因,我需要在每個列名稱周圍加引號。對於這個我修改我的查詢:
sqlstr = "SELECT 'SELECT ' || ARRAY_TO_STRING(ARRAY(SELECT ('"' || COLUMN_NAME::VARCHAR(50) || '"') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='{table}' AND COLUMN_NAME NOT IN ('the_geom') ORDER BY ORDINAL_POSITION), ', ') || ' FROM {table}';".format(** {
'table': name
})
這將返回一個錯誤:
ProgrammingError: could not find array type for data type unknown
它必須以Python和psycopg2報價有關。