__MagicPower
Do one thing and do it best!

导航

 
#!/usr/bin/env python
#--*-- coding:utf-8 --*--
__author__ = 'Kevin'

import MySQLdb as mdb
import sys

con = mdb.connect('localhost','root','redhat','test')

try:
    with con:
        cur = con.cursor()
        cur.execute("SELECT * FROM Writers")

        rows = cur.fetchall()

        for row in rows:
            print row
finally:
    con.close()

 Result:

(1L, 'Jack London')
(2L, 'Tik Tok')
(3L, 'Harry Potter')

 

#!/usr/bin/env python
#--*-- coding:utf-8 --*--
__author__ = 'Kevin'

import MySQLdb as mdb
import sys

con = mdb.connect('localhost','root','redhat','test')

try:
    with con:
        cur = con.cursor()
        cur.execute("SELECT * FROM Writers")

        numrows = int(cur.rowcount)

        for i in range(numrows):
            row = cur.fetchone()
            print row[0],row[1]
finally:
    con.close()

 

posted on 2016-01-05 14:50  __MagicPower  阅读(214)  评论(0编辑  收藏  举报