1.4.7 Retrieving Results

Once you have executed a SELECT statement you will want to retrieve the results. This is done using the cursor.fetchall() method:

cursor.execute("SELECT * FROM Test")
results = cursor.fetchall()

The results variable will always contain a tuple of tuples of fields. If the query matched no rows, result will be ((),). If it matched one row it will be in the form ((col1, col2, col3, etc),). If it matched more than one it will be in the form ((col1, col2, col3, etc), (col1, col2, col3, etc), etc )

You can print the results like this:

for row in cursor.fetchall():
    print "New Row"
    for field in row:
        print field

The cursor.fetchall() method will return the same results until another SQL query is executed using cursor.execute().