python and Postgresq

Andy dixon ajd at malcol.org
Mon Nov 23 05:36:26 EST 2009


"Diez B. Roggisch" <deets at nospam.web.de> wrote in message 
news:7mv62nF3hp171U1 at mid.uni-berlin.de...
> Andy dixon wrote:
>
>> Hi,
>>
>> Does anyone have a link to, or can provide an example script for using
>> python-pgsql (http://pypi.python.org/pypi/python-pgsql/) or if someone 
>> can
>> recommend an alternative, that would be fantastic.
>
> I'd recommend psycopg2.
>
> This is an introduction:
>
> http://www.devx.com/opensource/Article/29071
>
> But google yields tons more. And make sure you read the python db api 2.0
> spec, this should give you the general idea on how to work with Python &
> RDBMS, which is nicely abstracted away from the actual database.
>
>  http://www.python.org/dev/peps/pep-0249/
>
> Diez

Amazing. Works like a charm!

Thanks..

I used the code (stripping out certain bits) if anyone else may find it 
useful:

#!/usr/bin/env python
import psycopg

def main():
        connection = psycopg.connect('host=<HOST> dbname=<DB> user=<USER> 
password=<PASSWORD>')
        mark = connection.cursor()
        query='SELECT * FROM table'
        mark.execute(query)
        record = mark.fetchall()
        for i in record:
                print i
        return

if __name__ == '__main__':
     main()
 




More information about the Python-list mailing list