简体   繁体   English

在 Python For 循环中优化 SQL 执行

[英]optimizing SQL execution in Python For loop

I'm learning Python and this sqlite3 code is a bit align to me.我正在学习 Python,这个 sqlite3 代码有点适合我。 Is Python going to optimize and run the SQL statement once? Python 会优化并运行一次 SQL 语句吗?

for row in c.execute('SELECT * FROM stocks ORDER BY price'):
    print row

As I commented it will run once, the relevant part from the docs The for statement :正如我所评论的,它将运行一次,文档中的相关部分for 语句

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.为 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.当项目用完时(即当序列为空时),else 子句中的套件(如果存在)将被执行,并且循环终止。

Emphasis mine.强调我的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM