0
import dbapi2
conn = dbapi2.connect("db", autocommit=True)

def fetch_generator():
    cursor = conn.cursor()
    for res in cursor.execute('select * from table'):
        yield res

def generator_1():
    for x in fetch_generator():
        yield x

def generator_2():
    for x in generator_1():
        yield x

if __name__ == '__main__':
    for x in generator_2():
        print(x)

This throws an error at for x in fetch_generator()

Error: dbapi2.InterfaceError: Attempted to use a closed cursor

Why is the cursor closing with the chain of generators? Is is a scope issue and are context managers one solution? How do I keep the cursor from having del or close called on it automatically -- somewhere not sure when -- and keep it open.

Jason
  • 1,974
  • 24
  • 19
  • 1
    This is neither valid Python nor valid SQL. When I fix the obvious errors (`sqlite3` instead of `dbapi2`, no `autocommit`, `cursor`, table name, test data), the generators work for me. Please provide a [mcve]. – CL. Aug 08 '18 at 08:34
  • moving the conn = dbapi2.connect to the inside of fetch_generator – Jason Aug 09 '18 at 15:57

0 Answers0