0

私は pyscripter を使用して外部から PostgreSQL を呼び出し、ネットワークをルーティングします。これが私のコードです。

import sys, os

#set up psycopg2 environment
import psycopg2

#driving_distance module
query = """
    select *
    from driving_distance ($$
        select
            gid as id,
            source::int4 as source,
            target::int4 as target,
            cost::double precision as cost
        from network
        $$, %s, %s, %s, %s
    )
"""

#make connection between python and postgresql
conn = psycopg2.connect("dbname = 'TC_area' user = 'postgres' host = 'localhost' password = 'xxxx'")
cur = conn.cursor()

#count rows in the table
cur.execute("select count(*) from network")
result = cur.fetchone()
k = result[0] + 1                #number of points = number of segments + 1

#run loops
rs = []
i = 1
while i <= k:
    cur.execute(query, (i, 1000000, False, True))
    rs.append(cur.fetchall())
    i = i + 1

#import csv module
import csv

j = 0
h = 0
ars = []
element = list(rs)

#export data to every row
with open('distMatrix.csv', 'wb') as f:
    writer = csv.writer(f, delimiter = ',')
    while j <= k - 1:
        while h <= k - 1:
            rp = element[j][h][1]
            ars.append(rp)
            h = h + 1
        else:
            h = 0
            writer.writerow(ars)
            ars = []
        j = j + 1

conn.close()

結果は良好ですが、PostgreSQL のDriving_distance関数でreverse_cost関数を使用したい場合は、「cost::double precision as cost」の下に 1 行追加するだけです。

rcost::double precision as reverse_cost

この行を追加した後、このエラー ボックスが表示されました。

ここに画像の説明を入力

そしてpython IDLEのエラーメッセージ、

Traceback (most recent call last):


File "C:\Users\Heinz\Documents\pyscript\postgresql\distMatrix.py.py", line 47, in <module>
    cur.execute(query, (i, 1000000, False, True))
ProgrammingError: 錯誤:  在"語法錯誤"附近發生 rcost
LINE 7:             rcost::double precision as reverse_cost
                    ^
QUERY:  
        select
            gid as id,
            source::int4 as source,
            target::int4 as target,
            cost::double precision as cost
            rcost::double precision as reverse_cost
        from network

PS。このネットワークのテーブルを変更したため、「rcost」列があり、ビューの一部は次のとおりです。

ここに画像の説明を入力

pgAdmin 内でコードを実行すると、正しい結果が得られました。

SELECT * FROM driving_distance('
SELECT gid as id,
    source::int4 AS source, 
    target::int4 AS target,
    cost::double precision as cost,
    rcost::double precision as reverse_cost
    FROM network',
3, 10000000, false, True);

ここに画像の説明を入力

しかし、ループを実行するには Python が必要です。どうすればこの問題を解決できますか?</p>

PS。関数の両方のブール値を FALSE に設定すると、理論的にはプログラムは rcost を無視し、コストのみから計算された回答を返しますが、それでも同じエラーが発生します。

ここに画像の説明を入力

問題はrcostに起因するようです。

Windows 8.1 x64でPostgreSQL 8.4、python 2.7.6を使用しています。


更新#1

スクリプトの 2 行を変更したところ、機能しました。

cost::double precision as cost,                    #need to add a trailing comma if followed by rcost

cur.execute(query, (i, 100000000000, False, True)) #the range must be greater than max of rcost
4

1 に答える 1

0

「cost::double precision as cost」という行には、末尾のコンマが必要です。

于 2014-04-17T13:48:52.377 に答える