python数据库的问题

python数据库的问题

#!/usr/bin/python
import _mysql   
from MySQLdb.constants import FIELD_TYPE
db=_mysql.connect(host="localhost",user="xxxx", passwd="xxx",db="python")
db.query("""select * from my  """)
r=db.store_result()
res=r.fetch_row()
while res :

   print res[0]
   print res[1]
   print res[2]
   print res[3]

执行结果:

[bxrt@bxrt python]$ python mysql_dc
('1', '13810304521', 'babywolf', 'beijing', '2006-10-16')
Traceback (most recent call last):
  File "mysql_dc", line 16, in ?
    print res[1]
IndexError: tuple index out of range
其实我是有2行记录的,为什么不循环啊!

问题还是自己解决吧!


原来, res.fetch_row()  的默认参数是只选取 结果中的一行记录显示
原文:fetch_row() takes some additional parameters. The first one is, how many rows (maxrows) should be returned. By default, it returns one row. It may return fewer rows than you asked for, but never more. If you set maxrows=0, it returns all rows of the result set. If you ever get an empty tuple back, you ran out of rows.

想要解决呢就看下边的:
fetch_row(...)
     |      fetch_row([maxrows, how]) -- Fetches up to maxrows as a tuple.
     |      The rows are formatted according to how:
     |
     |          0 -- tuples (default)    ---默认一行
     |          1 -- dictionaries, key=column or table.column if duplicated   ----参数1,代表所有结果
     |          2 -- dictionaries, key=table.column        ---暂时不知道
提出问题----解决问题------高手