我有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
所有的查询都有相同的列数,它们是相同类型(和顺序)? –