I had to write a quick feed parser using psycopg before and was wondering why stuff didn't save.
Turns out I was missing a very important line at the end... connection.commit() !
A quick example below shows how to use psycopg.
import psycopg2
class SaveStuff():
connection = None
cursor = None
def __init__(self):
# Connect to an existing database
self.connection = psycopg2.connect("dbname=yourdbname user=username host=twigstechtips.blogspot.com")
self.cursor = self.connection.cursor()
def __del__(self):
if self.cursor is not None:
self.cursor.close();
if self.connection is not None:
self.connection.close();
def save(self, items):
if self.cursor is None:
raise Exception("Invalid connection to database.")
# Delete existing
self.cursor.execute("DELETE FROM table_name")
# Fill in the new values
for item in items:
sql = "INSERT INTO table_name (id, name, weight) VALUES (%s, %s, %s)"
self.cursor.execute(sql, (item.id, item.title, item.order))
# Write the new info to db
self.connection.commit()
[ Source ]