Skip to main content
thank you , and I have edited something personal in your answer, thanks a lot!
Source Link

First, never swallow an exception, or catch all exceptions. Your connection code should be more like:

import sys
import psycopg2

conn = None
try:
    conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = 'ntubse40'"'****'")
except psycopg2.DatabaseError, ex:
    print 'I am unable to connect the database: " + ex
    sys.exit(1)

Now, you have a connection. Running SQL is trivial ... as the psycopg2 tutorial would show you.

curs = conn.cursor()
curs.execute("SELECT ...")
for row in curs:
   print row

You can be much more sophisticated about how you output row data, etc. The manual has lots of useful examples.

First, never swallow an exception, or catch all exceptions. Your connection code should be more like:

import sys
import psycopg2

conn = None
try:
    conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = 'ntubse40'")
except psycopg2.DatabaseError, ex:
    print 'I am unable to connect the database: " + ex
    sys.exit(1)

Now, you have a connection. Running SQL is trivial ... as the psycopg2 tutorial would show you.

curs = conn.cursor()
curs.execute("SELECT ...")
for row in curs:
   print row

You can be much more sophisticated about how you output row data, etc. The manual has lots of useful examples.

First, never swallow an exception, or catch all exceptions. Your connection code should be more like:

import sys
import psycopg2

conn = None
try:
    conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = '****'")
except psycopg2.DatabaseError, ex:
    print 'I am unable to connect the database: " + ex
    sys.exit(1)

Now, you have a connection. Running SQL is trivial ... as the psycopg2 tutorial would show you.

curs = conn.cursor()
curs.execute("SELECT ...")
for row in curs:
   print row

You can be much more sophisticated about how you output row data, etc. The manual has lots of useful examples.

Source Link

First, never swallow an exception, or catch all exceptions. Your connection code should be more like:

import sys
import psycopg2

conn = None
try:
    conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = 'ntubse40'")
except psycopg2.DatabaseError, ex:
    print 'I am unable to connect the database: " + ex
    sys.exit(1)

Now, you have a connection. Running SQL is trivial ... as the psycopg2 tutorial would show you.

curs = conn.cursor()
curs.execute("SELECT ...")
for row in curs:
   print row

You can be much more sophisticated about how you output row data, etc. The manual has lots of useful examples.