#!/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()