python - looping through dynamic table -


how loop through dynamic database table?

i have situation need loop through table n rows while looping adds rows in same table , should loop until old , newly added rows not iterated end.

for example have created prototype

import sqlite3 lite import sys con = none  try:     con = lite.connect('dynamiciteration.db')      cur = con.cursor()     cur.execute("drop table if exists tbl")     cur.execute("create table tbl (id integer primary key, roll text)")     cur.execute("insert tbl (roll) values ('1')")     con.commit() except lite.error, e:     print "error %s:" % e.args[0]     sys.exit(1)   cur.execute("select roll tbl") rows = cur.fetchall() print len(rows) j = 0  row in rows:     #print 'iteration '+str(j)     j = j + 1     try:         cur.execute("insert tbl (roll) values ('"+str(j)+"')")         con.commit()         if(j < 100):             cur.execute("select roll tbl")             rows = cur.fetchall()             print len(rows)     except lite.error, e:         print "error %s:" % e.args[0]         sys.exit(1) 

where added termination condition if (j < 100). problem rows database object not updated other variables, otherwise work well.

any solution problem?

you re-binding rows list new result set, , for loop doesn't ever see such rebind. it's if did:

somelist = [1, 2, 3] in somelist:     somelist = [1, 2, 3, 4, 5]     print 

this ever print numbers 1, 2 , 3.

you use while loop:

index = 0 while index < len(rows)     row = rows[index]     index += 1 

now rows dereferenced each time while condition checked, when row set each iteration.

but have careful ordering make sure new rows added end of list result database.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -