2

I have imported one shapefile named tc_bf25 using qgis, and the following is my python script typed in pyscripter,

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)

I got the error at the second cur.execute(query),

enter image description here

But I go to pgAdmin to check result, even though no error occurs, the first cur.execute(query) didn't add new columns in my table.

What mistake did I make? And how to fix it?

I am working with postgresql 8.4, python 2.7.6 under Windows 8.1 x64.

5
  • 1
    Could you confirm that your code is correct please, as your first two queries are exactly the same. Was that deliberate? Commented Apr 1, 2014 at 15:38
  • What happens if you run conn.commit() between queries? Commented Apr 3, 2014 at 9:28
  • @Talvalin sorry for late reply, what do you mean in "conn.commit()"? Commented Apr 16, 2014 at 8:46
  • 1
    When using psycopg2, autocommit is set to False by default. The first two statements both refer to table tc_bf25, but the first statement makes an uncommitted change to the table. So try running conn.commit() between statements to see if this resolves the issue Commented Apr 16, 2014 at 8:56
  • @Talvalin I tried to add "conn.commit()" between every different queries, and run the python script. Pyscripter run it successfully, but as I try to use pgadmin to see one of the lately generated tables, pgadmin crashed, so I think there should be better way to do this. Commented Jun 9, 2014 at 8:41

3 Answers 3

5

When using psycopg2, autocommit is set to False by default. The first two statements both refer to table tc_bf25, but the first statement makes an uncommitted change to the table. So try running conn.commit() between statements to see if this resolves the issue

Sign up to request clarification or add additional context in comments.

1 Comment

If it is now possible to run the script via Pyscripter, then please accept this answer since it has resolved the original issue. If the executed queries are causing an issue with Pgadmin, then try again by running each query in turn in Pgadmin to see where any problems occur.
3

You should run each statement individually. Do not combine multiple statements into a semicolon separated series and run them all at one. It makes error handling and fetching of results much harder.

If you still have the problem once you've made that change, show the exact statement you're having the problem with.

Comments

0

Just to add to @Talvalin you can enable auto-commit by adding

psycopg2.connect("dbname='mydb',user='postgres',host ='localhost',password = '****'")
conn.autocommit = True

after you connect to your database using psycopg2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.