[英]Python error as using rcost to route in PostgreSQL
I use pyscripter to call PostgreSQL from outside and route my network, and here's my code, 我使用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()
The result is good, but if I want to use the reverse_cost function in the function driving_distance in PostgreSQL, I just add one line below the 'cost::double precision as cost', 其结果是好的,但如果我想使用reverse_cost功能在PostgreSQL的功能driving_distance,我只是下面加一条线“成本::双精度为代价”,
rcost::double precision as reverse_cost
I got this error box popped out after I added this line, 添加此行后,弹出此错误框,
and the error message in python IDLE, 以及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. PS。 I have altered the table of this network, thus it does have a 'rcost' column, and here's the part of view,
我已经更改了该网络的表格,因此它确实有一个“ rcost”列,这是视图的一部分,
If I execute the code inside pgAdmin, I could successfully get the right result, 如果我在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);
But I need python to do the loop, how can I solve this problem? 但是我需要python来做循环,如何解决这个问题?
PS. PS。 If I set both boolean in the function to FALSE, theoretically the program will ignore rcost and return answers calculated from cost only, but I still got the same error,
如果我将函数中的两个布尔值都设置为FALSE,理论上该程序将忽略rcost并仅返回根据cost计算得出的答案,但是我仍然遇到相同的错误,
The problem seems results from rcost . 问题似乎是rcost造成的 。
I am using PostgreSQL 8.4, python 2.7.6 under Windows 8.1 x64. 我正在Windows 8.1 x64下使用PostgreSQL 8.4,python 2.7.6。
UPDATE#1 更新#1
I changed 2 lines in my script and then it works, 我在脚本中更改了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
您的行“ cost :: double precision as cost”需要结尾的逗号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.