#!/usr/bin/python

import psycopg2
import sys
import ConfigParser

config = ConfigParser.ConfigParser()
config.read("db.cfg")

try:
	PG_HOST = config.get("postgres", "host")
	PG_USER = config.get("postgres", "user")
	PG_PASSWD = config.get("postgres", "passwd")
	PG_DATABASE = config.get("postgres", "database")

except (ConfigParser.NoSectionError, ConfigParser.NoOptionError), e:
	print "error: config: %s" % (str(e))
	sys.exit(1)

try:
	db = psycopg2.connect(host=PG_HOST, user=PG_USER, password=PG_PASSWD, database=PG_DATABASE)
except Exception, e:
	print "error: pgsql connect: %s" % (str(e))
	sys.exit(1)

if __name__ == "__main__":
	try:
		#cursor = db.cursor(cursor_factory=psycopg2.extras.DictCursor)
		cursor = db.cursor()
		cursor.execute("INSERT INTO actor (id,name) VALUES (%s, %s)", (10000, "HHG"))
		db.commit()

		cursor.execute("SELECT * FROM actor LIMIT 10;")

		for row in cursor:
			print row

	except Exception, e:
		print "error: %s" % (str(e))
	finally:
		cursor.close()
		db.close()

