python遍历数据

 1 #coding=utf-8
 2 import MySQLdb
 3 conn = MySQLdb.Connect(host = '127.0.0.1',port=3306,user='root',passwd='',db='test',charset='utf8')
 4 cursor = conn.cursor()
 5 sql = "select * from orders order by id asc"
 6 cursor.execute(sql)
 7 
 8 #获取所有数据,遍历所有的数据
 9 rs = cursor.fetchall()
10 for row in rs:
11     print "id=%d,order_id=%d,product_id=%d" % row
12 
13 cursor.close()
14 conn.close()

 

posted @ 2015-08-30 22:00  北京流浪儿  阅读(508)  评论(0编辑  收藏  举报