我已導入使用QGIS命名tc_bf25一個shape文件執行線,下面是我的Python腳本pyscripter類型,使用Python PostgreSQL裏
import sys
import psycopg2
conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = '****'")
cur = conn.cursor()
query = """
ALTER TABLE tc_bf25 ADD COLUMN source integer;
ALTER TABLE tc_bf25 ADD COLUMN target integer;
SELECT assign_vertex_id('tc_bf25', 0.0001, 'the_geom', 'gid')
;"""
cur.execute(query)
query = """
CREATE OR REPLACE VIEW tc_bf25_ext AS
SELECT *, startpoint(the_geom), endpoint(the_geom)
FROM tc_bf25
;"""
cur.execute(query)
query = """
CREATE TABLE node1 AS
SELECT row_number() OVER (ORDER BY foo.p)::integer AS id,
foo.p AS the_geom
FROM (
SELECT DISTINCT tc_bf25_ext.startpoint AS p FROM tc_bf25_ext
UNION
SELECT DISTINCT tc_bf25_ext.endpoint AS p FROM tc_bf25_ext
) foo
GROUP BY foo.p
;"""
cur.execute(query)
query = """
CREATE TABLE network1 AS
SELECT a.*, b.id as start_id, c.id as end_id
FROM tc_bf25_ext AS a
JOIN node AS b ON a.startpoint = b.the_geom
JOIN node AS c ON a.endpoint = c.the_geom
;"""
cur.execute(query)
query = """
ALTER TABLE network1 ADD COLUMN shape_leng double precision;
UPDATE network1 SET shape_leng = length(the_geom)
;"""
cur.execute(query)
我在第二CUR得到了錯誤。執行(查詢),
但我去pgAdmin的檢查結果,即使沒有出現錯誤,第一cur.execute(查詢)沒有在我的表中添加新的列。
我都做什麼錯?以及如何解決它?
我在PostgreSQL 8.4,蟒蛇2.7.6在Windows 8.1 x64的工作。
@CraigRinger好的,謝謝您的提醒。 – Heinz
請確認您的代碼是否正確,因爲您的前兩個查詢完全相同。那是故意的嗎? – Talvalin
@Tvalvalin感謝您的提醒,並且我編輯了我的帖子。 – Heinz