Interview Questions

Python - Mysql DB connectivity Example

Use the following code to connect and fetch records to MysqlDB from Python

'''
Created on Aug 5, 2013

@author: Administrator
'''
from MySQLdb import *

class DAO(object):
    '''
    classdocs
    '''


    def __init__(self):
        print "hi"
       
    def dbAccess(self):
         db = connect("localhost", "root", "mysql", "techDB")
         cursor = db.cursor()
         cursor.execute("SELECT * from Student")
         row = cursor.fetchone()
         while row is not None:
              print(row)
              row = cursor.fetchone()
         db.close()  
         
dao = DAO()
dao.dbAccess();