python iterate row one by one mysql field -
i trying programme have 'table= qwes' , 'field1= first, field2= second". need attain each row once, must pass through status 'if row has got hello, true else false'.
i used limit , needed increment offset , has loop next row finding true or false. likewise should end till rows finished.
import mysqldb db = mysqldb.connect(host="localhost", # host, localhost user="root", # username passwd="mysql", # password db="sakila") # name of info base of operations cursor = db.cursor() qas = 0 while(qas < 100) qas = qas + 1 posts = "select * ques limit 1 offset %s " %(qas) cursor.execute(posts) db.commit() keywords=[] a='hello' in cursor_posts.fetchall(): keywords.append(i[0]) if in keywords: print true else: print false raw_input("please press come in continue") please help me in fixing program; want while loop execute until rows available. due lack of knowledge made default 100.
my recommendation somehting this:
import mysqldb db = mysqldb.connect(host="localhost", # host, localhost user="root", # username passwd="mysql", # password db="sakila") # name of info base of operations cursor = db.cursor() posts = "select * ques limit 100" cursor.execute(posts) db.commit() a='hello' counter = 0 j in cursor.fetchall(): counter += 1 if in j[0]: print ("row " + str(counter) + "= " + j[0] + " => true") else: print ("row " + str(counter) + "= " + j[0] + " => false") raw_input("please press come in continue") basically query database 1 time 100 records... loop through fetchall() of executed statement , check if 'hello' in j[0] post be... assuming first column in database post... if id move j[1]... run debug on goes , @ in j when go through loop.. should simple prepare if doesn't work
python mysql
No comments:
Post a Comment