Python论坛  - 讨论区

标题:[python-chinese] Fw: python mysqldb 数据库问题

2006年10月17日 星期二 10:08

babywolf_2 babywolf_2在163.com
星期二 十月 17 10:08:41 HKT 2006





babywolf_2
2006-10-17
Ìõ¼þ:
Êý¾Ý¿âÖдæÔÚ2Ìõ¼Ç¼,
¼Ç¼×ֶηֱðÊÇ id, pn,name,pro,date
Ä¿µÄ:
  ʹÓÃpython Á¬½ÓÊý¾Ý¿â,Ñ¡È¡ÄÚÈݲ¢ÏÔʾ³öÀ´
#!/usr/bin/python
import _mysql   
db=_mysql.connect(host="localhost",user="xxxx", passwd="xxx",db="python")
db.query("""select pn,name,pro,date 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
( '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

ÒÔÎĵµÉÏ˵µÄmysqldbºÍcµÄapi ÀàËÆ, Ϊʲô²»Ñ­»·ÏÔʾ,²¢ÇÒÏÔʾ´íÎó,Ôª×éÔ½½çÄØ??  ÎÒ¸Õ¸Õѧϰpython Çë¶àÖ¸½Ì

ÁíÍâ,ÎÒ¿´µ½ÍøÉϲ»ÉÙʹÓÃpthon Á¬½ÓÊý¾Ýmysq'l ×öµÄ²Ù×÷,ºÃ¶à¶¼ÊÇʹÓÃcursor ,¹Ù·½Îĵµ²é²»µ½ÕâÑùµÄ!







babywolf_2
2006-10-17
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061017/d7af7b4d/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月17日 星期二 10:12

刘鑫 march.liu在gmail.com
星期二 十月 17 10:12:31 HKT 2006

http://kinterbasdb.sourceforge.net/dist_docs/Python-DB-API-2.0.html
cusorÊÇPYthon DBAPI ±ê×¼²Ù×÷¶ÔÏó¡£ÒÔÉÏÁ´½ÓÊÇFirebirdÇý¶¯KInterbasdbÕ¾µã¶ÔDBAPI±ê×¼×öµÄ¾µÏñÒ³¡£

ÔÚ06-10-17£¬babywolf_2 <babywolf_2在163.com> дµÀ£º
>
>
>
>  ------------------------------
>  babywolf_2
> 2006-10-17
>   Ìõ¼þ:
> Êý¾Ý¿âÖдæÔÚ2Ìõ¼Ç¼,
> ¼Ç¼×ֶηֱðÊÇ id, pn,name,pro,date
> Ä¿µÄ:
>   ʹÓÃpython Á¬½ÓÊý¾Ý¿â,Ñ¡È¡ÄÚÈݲ¢ÏÔʾ³öÀ´
> #!/usr/bin/python
> import _mysql
> db=_mysql.connect(host="localhost",user="xxxx", passwd="xxx",db="python")
> db.query("""select pn,name,pro,date 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
> ( '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
>
> ÒÔÎĵµÉÏ˵µÄmysqldbºÍcµÄapi ÀàËÆ, Ϊʲô²»Ñ­»·ÏÔʾ,²¢ÇÒÏÔʾ´íÎó,Ôª×éÔ½½çÄØ??  ÎÒ¸Õ¸Õѧϰpython Çë¶àÖ¸½Ì
>
> ÁíÍâ,ÎÒ¿´µ½ÍøÉϲ»ÉÙʹÓÃpthon Á¬½ÓÊý¾Ýmysq'l ×öµÄ²Ù×÷,ºÃ¶à¶¼ÊÇʹÓÃcursor ,¹Ù·½Îĵµ²é²»µ½ÕâÑùµÄ!
>
>
>
>
>  ------------------------------
>  babywolf_2
> 2006-10-17
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
»¶Ó­·ÃÎÊ£º
http://blog.csdn.net/ccat

ÁõöÎ
March.Liu
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061017/acde5a12/attachment-0001.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月17日 星期二 10:16

Ivan Diao adieu在adieu.cn
星期二 十月 17 10:16:03 HKT 2006

¹Ù·½ÎĵµÔÚhttp://www.python.org/dev/peps/pep-0249/¡£ÁíÍâ×îºóÒ»¶Î´úÂë¿ÉÒԸijɣº
result=r.fetchall() 
for (res in result):
    print res


Date: Tue, 17 Oct 2006 10:12:31 +0800From: march.liu在gmail.comTo: python-chinese在lists.python.cnSubject: Re: [python-chinese] Fw: python mysqldb Êý¾Ý¿âÎÊÌâhttp://kinterbasdb.sourceforge.net/dist_docs/Python-DB-API-2.0.htmlcusorÊÇPYthon DBAPI ±ê×¼²Ù×÷¶ÔÏó¡£ÒÔÉÏÁ´½ÓÊÇFirebirdÇý¶¯KInterbasdbÕ¾µã¶ÔDBAPI±ê×¼×öµÄ¾µÏñÒ³¡£
ÔÚ06-10-17£¬babywolf_2 <babywolf_2在163.com> дµÀ£º 


 
 




babywolf_2
2006-10-17



Ìõ¼þ:
Êý¾Ý¿âÖдæÔÚ2Ìõ¼Ç¼,
¼Ç¼×ֶηֱðÊÇ id, pn,name,pro,date
Ä¿µÄ:
  ʹÓÃpython Á¬½ÓÊý¾Ý¿â,Ñ¡È¡ÄÚÈݲ¢ÏÔʾ³öÀ´
#!/usr/bin/pythonimport _mysql   db=_mysql.connect(host="localhost",user="xxxx", passwd="xxx",db="python")db.query("""select pn,name,pro,date 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( '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ÒÔÎĵµÉÏ˵µÄmysqldbºÍcµÄapi ÀàËÆ, Ϊʲô²»Ñ­»·ÏÔʾ,²¢ÇÒÏÔʾ´íÎó,Ôª×éÔ½½çÄØ??  ÎÒ¸Õ¸Õѧϰpython Çë¶àÖ¸½Ì
 
ÁíÍâ,ÎÒ¿´µ½ÍøÉϲ»ÉÙʹÓÃpthon Á¬½ÓÊý¾Ýmysq'l ×öµÄ²Ù×÷,ºÃ¶à¶¼ÊÇʹÓÃcursor ,¹Ù·½Îĵµ²é²»µ½ÕâÑùµÄ!
 
 
 
 



babywolf_2
2006-10-17_______________________________________________python-chinesePost: send python-chinese在lists.python.cn Subscribe: send subscribe to python-chinese-request在lists.python.cnUnsubscribe: send unsubscribe to   python-chinese-request在lists.python.cnDetail Info: http://python.cn/mailman/listinfo/python-chinese-- »¶Ó­·ÃÎÊ£ºhttp://blog.csdn.net/ccatÁõöÎMarch.Liu
_________________________________________________________________
Check the weather nationwide with MSN Search: Try it now!
http://search.msn.com/results.aspx?q=weather&FORM;=WLMTAG
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061016/d02d49a1/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月17日 星期二 18:36

Linker Lin linkerlin88在gmail.com
星期二 十月 17 18:36:09 HKT 2006

myCon = MySQLdb.connect(host=host, port=port, user=user, passwd=passwd)
cursor = myCon.cursor()
ret = 0
SQLString = "SELECT * FROM table_name;"
ret=cursor.execute(SQLString)
if ret : print "OK!"
for record in cursor:
    print record


On 10/17/06, Ivan Diao <adieu在adieu.cn> wrote:
>
>
>
> 官方文档在http://www.python.org/dev/peps/pep-0249/。另外最后一段代码可以改成:
>
> result=r.fetchall()
> for (res in result):
>     print res
>
>
>  ------------------------------
> Date: Tue, 17 Oct 2006 10:12:31 +0800
> From: march.liu在gmail.com
> To: python-chinese在lists.python.cn
> Subject: Re: [python-chinese] Fw: python mysqldb 数据库问题
>
> http://kinterbasdb.sourceforge.net/dist_docs/Python-DB-API-2.0.html
> cusor是PYthon DBAPI 标准操作对象。以上链接是Firebird驱动KInterbasdb站点对DBAPI标准做的镜像页。
>
> 在06-10-17,*babywolf_2* <babywolf_2在163.com> 写道:
>
>
>
>  ------------------------------
>  babywolf_2
> 2006-10-17
>   条件:
> 数据库中存在2条记录,
> 记录字段分别是 id, pn,name,pro,date
> 目的:
>   使用python 连接数据库,选取内容并显示出来
> #!/usr/bin/python
> import _mysql
> db=_mysql.connect(host="localhost",user="xxxx", passwd="xxx",db="python")
> db.query("""select pn,name,pro,date 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
> ( '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
>
> 以文档上说的mysqldb和c的api 类似, 为什么不循环显示,并且显示错误,元组越界呢??  我刚刚学习python 请多指教
>
> 另外,我看到网上不少使用pthon 连接数据mysq'l 做的操作,好多都是使用cursor ,官方文档查不到这样的!
>
>
>
>
>  ------------------------------
>  babywolf_2
> 2006-10-17
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to   python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
> --
> 欢迎访问:
> http://blog.csdn.net/ccat
>
> 刘鑫
> March.Liu
>
>
> ------------------------------
> Check the weather nationwide with MSN Search Try it now!<http://search.msn.com/results.aspx?q=weather&FORM;=WLMTAG>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
Linker M Lin
linkerlin88在gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061017/87d35d49/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号