How to use Python 2.7 to connect to Postgres db?

Experts,

I would like to use Python272 to connect to a Postgres db. I found instructions here:
http://wiki.python.org/moin/UsingDbApiWithPostgres?action=print   to createa database etc. I looked at the directions and downloaded DB-API, psycopg2 modules. Then I used the Python interperator to enter
Python DB-API:

import psycopg2 as dbapi2
db = dbapi2.connect (database="python", user="python", password="python")
cur = db.cursor()

I get a Syntax error: invalid syntax at
cur = db.cursor()
**screen shot attached.

I really don't understand how I could attach to the database....I have not created the db yet in the directions...but this is how the directions seem to read - in order.
postgres.png
MissyMadi2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peprCommented:
You have probably already found it, but you have missing closing parenthesis on the line with

db = dbapi2.connect (...

at the screen shot.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MissyMadi2Author Commented:
Thanks....second pair of eyes always helps :)

I'm still not sure if I'm using the Python interperator correctly for the commands for DB-API2.0.  From the instructions http://wiki.python.org/moin/UsingDbApiWithPostgres?action=print , I am to run these commands from the Python interperator??
0
peprCommented:
I am sorry, I cannot help with PostgreSQL and its interface from Python.  Probably someone else will help you here.  If not, send a request for the attention to the topic-area administrator.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

peprCommented:
But yes, you are expected to put the SQL command string into your cur.execute() as shown at the page:

cur.execute ("""CREATE TABLE versions (released date, version varchar, status varchar)""")

Open in new window


The triple quotes are used here to suggest that you can use the multiline string for the purpose (the SQL command may look more readable), like this:

cur.execute ("""CREATE TABLE versions (
                   released date, 
                   version varchar, 
                   status varchar
                )""")

Open in new window


Here the CREATE TABLE does not fill the cur object with loopable information, but for example:

cur.execute ("SELECT * FROM versions")

Open in new window


makes the cur object be accessor to he data retrieved from the database.  (It seems the interface to PostgreSQL is at least similar to other database interfaces...)

rows = cur.fetchall()                 # here you get the list of rows
for i, row in enumerate(rows):
    print "Row", i, "value = ", row

Open in new window


If the interface was modernized, you can also try:

for i, row in enumerate(cur):
    print "Row", i, "value = ", row

Open in new window


without using the .fetchall().  It is because the cur often works as the iterator object.
0
peprCommented:
Thanks for the points but you can usually be a bit slower in accepting, and discuss the problem more thoroughly also with other people who probably looked at your question but did not reacted yet (waiting where the discussion goes ;)

You can still add your comments here and it is likely it can be answered even after accepting (if it is related to the question).
0
MissyMadi2Author Commented:
OK. I thought I may have to open another question.

Using the mentioned URL to do the examples, I entered the SQL commands directly from the SQL Query, all data was created and retrieved, etc.  Simple....

I don't understand the DB-API2.0 side of this. I use the Python Interperator and enter cur.execute ("SELECT * FROM versions");

Shouldn't I see all records returned in the Pthyon Interperator screen?
0
peprCommented:
Yes, if the table "versions" is there, if it contains anything, and if no error happened.  Try to replace the for loop by simply printing the result of .fetchall():

...
rows = cur.fetchall()
print rows

Open in new window


When working in the interactive mode, you can use simply:

>>> rows

Open in new window


instead of print rows
0
MissyMadi2Author Commented:
Got it!! Thanks. Rows returned in Python. I think what happened is that my connection timed out??

My goal is to create a GUI for users to enter data and capture data and store it. We are currently performing manually and storing data in spreadsheets. Now I can apply the DB-API 20 code to gain access to the Postgres db.

How do I get to the GUI creation for the users? A form for the users to fill out to capture the data?
0
peprCommented:
For GUI, you have to choose one of the more possibilities. I recommend to focus on the cross-platform frameworks.  I tend to use the free software (another criterium).

Python comes with TkInter (the wrapper around the rather ol Tcl/Tk http://en.wikipedia.org/wiki/Tcl, http://docs.python.org/library/tkinter.html).  The cons is that the windows do not look natural (native) in the chosen OS.

I personally like WxPython, which is the wrapper around wxWidgets (http://www.wxpython.org/, http://www.wxwidgets.org/. You can get the advertised book [a.k.a. wxBook] also for free in PDF, legally).  The cons is that it there is no version for Python 3, yet.

There also is PyQt which is the wrapper around Qt framework (http://wiki.python.org/moin/PyQt, http://qt.nokia.com/products/).  The cons is more complex licensing.  I cannot speak for that.

If you never programmed a GUI application, you should be prepared to spend some time when learning the chosen GUI framework.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PostgreSQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.