Reputation: 1291
This is a general question, consider I have a table with a number of columns like:
serial_number time number_1 number2
1 14:00:00 123 124
2 13:00:34 121 11111
with a million rows. I want to get the result of a query like:
SELECT * from table if time = "14:00:00";
the result of this is a tuple of tuples, how will I traverse the result and process it in python, can you please point to me some on doing this.
Upvotes: 0
Views: 1214
Reputation: 1291
I have mentioned this in a comment to @KT , just make it clear, this article http://blogs.wrox.com/article/using-the-python-database-apis/ from the book Using Python 2.6 and Python 3.1 by James Payne is the best resource on this topic that I have read till date.
Upvotes: 0
Reputation: 2985
Maybe something like this is what you're looking for:
c.execute("SELECT serial_number, time, number_1, number2 FROM table WHERE time = '14:00:00'")
sum_number_1 = 0
for serial_number, time, number_1, number2 in c:
print serial_number, time, number_1, number2
sum_number_1 += number_1
print sum_number_1
Cursor iterator support for DB API v2.0 clients is optional (http://legacy.python.org/dev/peps/pep-0249/#optional-db-api-extensions) but mostly supported. I tested it with psycopg2.
Upvotes: 0
Reputation: 11430
General question - general answer. You can traverse anything in Python using iteration:
for row in c.execute('SELECT * from table if time = "14:00:00"').fetchall():
print row
Upvotes: 3