1

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?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Shansal
  • 1,369
  • 5
  • 16
  • 26

1 Answers1

1

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')]
samplebias
  • 37,113
  • 6
  • 107
  • 103
  • (Disclosure: I am the APSW author) APSW 3.6.6.2 released in December 2008 added cursor.fetchall(). It is actually more characters to type than wrapping the cursor in list()! – Roger Binns May 08 '11 at 01:14