Problem with sqlite3 cursor and imbricated for loop

Charles V. charles.vejnar at unige.ch
Wed Nov 12 00:44:38 CET 2008


Hi,

I hope this is not already known. But Google wasn't any help. So here begins a 
script to explain my problem.

-------------------------
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('''create table stocks
(date text, trans text, symbol text,
 qty real, price real)''')
c.execute("insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)")
c.execute("insert into stocks values ('2006-01-06','BUY','RHAT',100,20.0)")
c.execute("insert into stocks values ('2006-01-07','BUY','RHAT',100,15.0)")
c.execute("insert into stocks values ('2006-01-08','BUY','RHAT',100,10.0)")
conn.commit()
c.execute("select * from stocks")
for s in c:
   print s[0]
   c.execute("select * from stocks where price<20")
   for s in c:
      print '  '+s[0]
c.close()
-------------------------

It is a adapted copy of the example in the Python documentation. But I was 
expecting the output as with MySQL engine but, here, I get only:
2006-01-05
  2006-01-07
  2006-01-08

It seems the second call to execute modify the first cursor. Is it normal ? 
How am I suppose to write this ?

Thanks

Charles



More information about the Python-list mailing list