免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2816 | 回复: 4
打印 上一主题 下一主题

python数据库的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-16 18:46 |只看该作者 |倒序浏览
#!/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行记录的,为什么不循环啊!

[ 本帖最后由 西门子 于 2006-10-16 21:45 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-10-17 11:41 |只看该作者
问题还是自己解决吧!


原来, 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        ---暂时不知道

论坛徽章:
0
3 [报告]
发表于 2006-10-25 14:33 |只看该作者
提出问题----解决问题------高手

论坛徽章:
0
4 [报告]
发表于 2009-03-29 23:31 |只看该作者
我也碰到过类似的问题:就是用fetchall函数的时候返回的turple超过了index,后来阅读英文文档关于MySQLdb的之后才知道fetchall是把每条记录用turple构造然后多条记录返回的话再以list装起所有的turple~~~

论坛徽章:
0
5 [报告]
发表于 2009-03-30 12:40 |只看该作者

回复 #4 jianpx 的帖子

请不要把若干年前的问题再翻出来把,谢谢 :)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP