Search code examples
pythondatabasesqliteapsw

Fetch All in APSW


You know there isn't any "fetchall" method for APSW. We can only use .next() method. So How can I take the result of an execute command with select query into a list?


Solution

  • Version 3.6.22 of apsw let me use cursor.fetchall() to retrieve all rows in a list:

    import apsw
    
    conn = apsw.Connection(':memory:')
    curs = conn.cursor()
    curs.execute("CREATE TABLE foo (id INTEGER, name VARCHAR(255))")
    curs.executemany("INSERT INTO foo VALUES (?, ?)", [(1, 'bar'),(2, 'baz')])
    print curs.execute("SELECT * FROM foo").fetchall()
    
    [(1, u'bar'), (2, u'baz')]
    

    If your version does not support this, but it supports .next(), can you just wrap the cursor in a list (iterating over the cursor)? This works for me:

    curs.execute("SELECT * FROM foo")
    print list(curs)
    
    [(1, u'bar'), (2, u'baz')]