A wrapper around psycopg2 to make common things easier.
pip install psycopgwrap
python-psycopgwrap Written by Sean Reifschneider <jafo@tummy.com> Placed in the public domain. No rights reserved. Please submit bugs/fixes at: http://github.com/linsomniac/python-psycopgwrap A wrapper around psycopg2 to make common things easier. It really doesn't follow the DB-API. It's mostly for me to experiment with other ways of talking to the database than the standard DB-API. I've used this in a number of projects. It's primary focus is convenience. Here are a few of the features: Query results can be accessed as a dictionary of columns (row['id']). Query results can be accessed as attributes (row.id). Query results can be accessed as lists (row[0]). Queries return indexable iterators, so you can do things like "for row in query", and "query(cmd)[0]". Note that indexes can only be larger than or equal to the last index. Query arguments are passed as discrete arguments, not as a tuple. Passing as a tuple always makes me want to do a "%" and if I accidentally do that it can open up a SQL injection. There's an "insert" helper that takes a table name and keyword arguments. Similarly there's a "dictinsert" helper that takes a dictionary of row:values. Examples: from psycopgwrap import Database as db db.connect('dbname=testdb') user = db.query('SELECT * FROM users WHERE name = %s', name) if user == None: print 'No such user "%s"' % name print 'User id: %s' % user.id # or user.id_ for row in db.query('SELECT * FROM status WHERE id = %s', 500): print 'id:', row.['id'] Other examples: db.insert('users', name = 'Al Bert') db.insert('users', { name : 'Al Bert' }) user_count = db.queryone('SELECT COUNT(*) FROM users')[0] # 0-th column if db.queryone("SELECT * FROM users WHERE name = 'Al Bert'"): print 'Al is in the house' else: print 'No such user' try: db.commit() except: db.rollback()