title: yield as opposed to while True: yield usage in python
body: <p>I wanted to build a helper function to get query results from sqlite, to use for comparison with results from another database.</p>
  
  <p>I wanted the user to have a simple interface, where he calls the function with his query and gets a result, without any other preparation.</p>
  
  <p>I ended up writing 2 functions:</p>
  
  <pre><code>import sqlite3 
  .
  .
  def sqlite_query_executor(query):
      conn=sqlite3.connect(':memory:')
      cur=conn.cursor()
  
      yield cur.execute(query).fetchall()[0][0]
  </code></pre>
  
  <p>And the one meant to be called by the user:</p>
  
  <pre><code>def query_sqlite(query):
  
      return next(sqlite_query_executor(query))
  </code></pre>
  
  <p>While tinkering with the first function, I initially wrote another version with a slightly different ending:</p>
  
  <pre><code>def sqlite_query_executor(query):
      conn=sqlite3.connect()
      cur=conn.cursor()
  
      while True:
          yield cur.execute(query).fetchall()[0][0]
  </code></pre>
  
  <p>Both versions seemed to work well, but I'm struggling with the mechanics. </p>
  
  <p>Could someone perhaps shed light on the difference? What is the correct pattern for such usecase?</p>