sqlite - yield as opposed to while True: yield usage in python -


i wanted build helper function query results sqlite, use comparison results database.

i wanted user have simple interface, calls function query , gets result, without other preparation.

i ended writing 2 functions:

import sqlite3  . . def sqlite_query_executor(query):     conn=sqlite3.connect(':memory:')     cur=conn.cursor()      yield cur.execute(query).fetchall()[0][0] 

and 1 meant called user:

def query_sqlite(query):      return next(sqlite_query_executor(query)) 

while tinkering first function, wrote version different ending:

def sqlite_query_executor(query):     conn=sqlite3.connect()     cur=conn.cursor()      while true:         yield cur.execute(query).fetchall()[0][0] 

both versions seemed work well, i'm struggling mechanics.

could perhaps shed light on difference? correct pattern such usecase?

the fetchall() work , should replaced fetchone():

def sqlite_query_executor(query):     conn=sqlite3.connect()     cur=conn.cursor()      while true:         yield cur.execute(query).fetchone()[0] 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -