Real time python app that works on a database

Viewed 3287

I am using postgreSQL with python and the SQL database is such that rows are added regularly. At present, the python program does not know if new data has been added (I used psycopg2 to read rows. But it reads till the end of rows and stops). How can I let my python program constantly search if new data has been added? Or can I let postgreSQL trigger python when a new row is added?

This is what I have currently:

def get_data():
    try:
        connect = psycopg2.connect(database="yardqueue", user="postgres", password="abcd", host="localhost", port="5432")
    except:
        print "Could not open database"
    cur = connect.cursor()
    cur.execute("SELECT id,position FROM container")
    rows = cur.fetchall()
    for row in rows:
        print "ID = ", row[0]
        print "Position = ", row[1]

As you see, when I run this, it stops once variable 'row' reaches the last row.

EDIT: Is there a way I can keep my python code running for a specified amount of time? If so, I can make it go through the database until I kill it.

2 Answers
Related