我有postgres表,我想在這些表上使用python運行PostgreSQL
腳本文件,然後在csv文件中寫入查詢結果。該腳本文件有多個以分號分隔的查詢;
。示例腳本如下所示閱讀和使用python postgres腳本
腳本文件:
--Duplication Check
select p.*, c.name
from scale_polygons_v3 c inner join cartographic_v3 p
on (metaphone(c.name_displ, 20) LIKE metaphone(p.name, 20)) AND c.kind NOT IN (9,10)
where ST_Contains(c.geom, p.geom);
--Area Check
select sp.areaid,sp.name_displ,p.road_id,p.name
from scale_polygons_v3 sp, pak_roads_20162207 p
where st_contains(sp.geom,p.geom) and sp.kind = 1
and p.areaid != sp.areaid;
當我運行Python代碼,它成功地執行沒有任何錯誤,但我面臨的問題是,編寫查詢的結果中一個csv文件。只有上次執行的查詢的結果才寫入csv文件。這意味着第一個查詢結果被第二個查詢覆蓋,第二個查詢結果被第三個覆蓋,直到最後一個查詢。
這裏是我的Python代碼:
import psycopg2
import sys
import csv
import datetime, time
def run_sql_file(filename, connection):
'''
The function takes a filename and a connection as input
and will run the SQL query on the given connection
'''
start = time.time()
file = open(filename, 'r')
sql = s = " ".join(file.readlines())
#sql = sql1[3:]
print "Start executing: " + " at " + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) + "\n"
print "Query:\n", sql + "\n"
cursor = connection.cursor()
cursor.execute(sql)
records = cursor.fetchall()
with open('Report.csv', 'a') as f:
writer = csv.writer(f, delimiter=',')
for row in records:
writer.writerow(row)
connection.commit()
end = time.time()
row_count = sum(1 for row in records)
print "Done Executing:", filename
print "Number of rows returned:", row_count
print "Time elapsed to run the query:",str((end - start)*1000) + ' ms'
print "\t ==============================="
def main():
connection = psycopg2.connect("host='localhost' dbname='central' user='postgres' password='tpltrakker'")
run_sql_file("script.sql", connection)
connection.close()
if __name__ == "__main__":
main()
什麼是錯我的代碼?
可能沒有幫助,但代碼看起來不錯。你正在打開文件模式'a',就像[這裏]完成(http://stackoverflow.com/questions/2363731/append-new-row-to-old-csv-file-python) – Matthias
執行腳本單次調用你只會得到最後執行的命令的結果(或者如果出現錯誤,則會出錯)。 – Abelisto
所有的查詢都有相同的列數,它們是相同類型(和順序)? –