Reputation: 19
I'm learning Python and this sqlite3 code is a bit align to me. Is Python going to optimize and run the SQL statement once?
for row in c.execute('SELECT * FROM stocks ORDER BY price'):
print row
Upvotes: 1
Views: 67
Reputation: 180481
As I commented it will run once, the relevant part from the docs The for statement:
for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]
The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the expression_list. The suite is then executed once for each item provided by the iterator, in the order of ascending indices. Each item in turn is assigned to the target list using the standard rules for assignments, and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty), the suite in the else clause, if present, is executed, and the loop terminates.
Emphasis mine.
Upvotes: 1