Problem with sqlite3 cursor and imbricated for loop

Gerhard Häring gh at ghaering.de
Wed Nov 12 10:29:37 EST 2008


Charles V. wrote:
> Hi,
> 
>> Both may be standard compliant, but if you're depending on
>> implementation details, you may still get different behaviour.
>> I'm pretty sure that MySQLdb always fetches the entire resultset from
>> the server. The sqlite3 module uses what would have been called
>> "server-side cursors" in real databases, i. e. it only fetches rows on
>> demand. To fetch everything in one go with the sqlite3 module, you have
>> to call fetchall() explicitly.
> 
> You are right: the default Cursor in MySQLdb fetches the complete set on the 
> client (It explains why I have a "correct" answer with MySQLdb). As having 
> multiple cursor isn't an option for me and using non-standard execute on the 
> connection neither, I tried to modify the Cursor class to store results on 
> the client side.
> 
> ----------------
> class NewCursor(sqlite3.Cursor):
> 	def __iter__(self):
> 		return iter(self.fetchall())
> 
> conn = sqlite3.connect(':memory:')
 > [...[]

> Do you think it is a good solution (any drawbacks ?) ?

It's at least incomplete. fetchone() and fetchmany() won't work any longer.

-- Gerhard




More information about the Python-list mailing list