PyGreSQL: This will be useful!

PyGreSQL is a python library that allows you to write python code to interact with a PostgreSQL database. I can see some utility here if I start wanting to write Django methods that do things like create views of some of my tables.

May be useful in not too long! Don't forget about it!



sudo apt-get install python-pygresql


>> import pg
>> conn = pg.connect('dbname', 'localhost', 5432, None, None, 'username', 'password')

>> pgqueryset = conn.query('SELECT * FROM tablename;')
>> results = pgqueryset.dictresults()
>> ooooh! aaaah!

>> conn.query('CREATE VIEW single_map_portrayal AS SELECT * FROM ncgmp_mapunitpolys WHERE mapunitpolys_id LIKE \'GMA%\';')
>> Holy smokes!

>> conn.query('CREATE TABLE mytable (n integer, t text)')
>> conn.inserttable('mytable', [(1,'Hello'),(2,'World')])
>> conn.query('SELECT * FROM mytable;')
(2 rows)

In combination with a csv reader:

>> import csv
>> file = open('ncgmp/data/dmu.csv')
>> reader = csv.reader(file)
>> list = []
>> for row in reader:
>> list.append(row)
>> conn.inserttable('descriptionofmapunits', list)