user4913191
user4913191

Reputation:

Python: Class does not print any statement while connecting to MYSQL Database

I am trying to create a class and methods to connect to Mysql database and update a table according to the requirements that I have received. My code updates the table without using any class and methods but stops working as soon as I include the class.

Example Code -

import MySQLdb
from opswareConnect import data
from echoResourceConfig import host, user, passwd, db

class echoResource(object):
def __init__(self):
    self.host = host
    self.user = user
    self.passwd = pwsswd
    self.db = db
    self.cursor = self.connect()

def connect(self):
    try:
        self.cnx = MySQLdb.connect(host, user, passwd, db)
        if cnx:
           print(self.cnx)
        self.cursor = self.cnx.cursor()
    except Exception, e:
        print "Error"
    return self.cursor

def updateResource(self, cursor):
    select_query = "SELECT resource_id, resource_name, support_contact_id FROM echo_resource WHERE resource_name = (%s);"
    update_query = "UPDATE echo_resource \
                       SET support_contact_id = ( \
                           SELECT contact_id FROM contacts WHERE last_name = (%s)) \
                     WHERE resource_name = (%s);"

    for row in data:
        arg1 = [row["system_name"],]
        arg2 = [row["fdc_inv_sa_team"]]
        row = self.cursor.execute(select_query, arg1)
        if row:
               data = self.cursor.fetchall()
               for res in data:
                    print(res)

               upd_row = self.cursor.execute(update_query, (arg2, arg1))
               if not upd_row:
                    print("Update failed")
               else:
                    self.cnx.commit()
                    print("update successful")
        else:
             print("No data found for "+row["system_name"])

Upvotes: 0

Views: 131

Answers (3)

user4913191
user4913191

Reputation:

Updated Code -

import MySQLdb
import logging
from opswareConnect import data
from echoResourceConfig import host, user, passwd, db, select_query, 
update_query, insert_query

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class echoResource(object):
def __init__(self):
    self.host = host
    self.user = user
    self.passwd = passwd
    self.db = db
    self.select_query = select_query
    self.update_query = update_query
    self.insert_query = insert_query


def connect(self):
    print "inside connect"
    try:
        self.cnx = MySQLdb.connect(host, user, passwd, db)
        if self.cnx:
            print("connection successful" , self.cnx)
        self.cursor = self.cnx.cursor()
    except Exception, e:
        print "Error"
    return self.cursor

def updateResource(self):
    try:
        for row in data:
            arg1 = [row["system_name"],]
            arg2 = [row["fdc_inv_sa_team"]]
        row = self.cursor.execute(self.select_query, arg1)
        print row
        if row:
        if row:
            data = self.cursor.fetchall()
            upd_row = self.cursor.execute(self.update_query, (arg2, str(datetime.now()), arg1))

            if not upd_row:
                print "upd_row", upd_row
                self.cursor.execute(self.insert_query, (arg1, arg2, "Update Failed", str(datetime.now())))
                self.cnx.commit()
            else:
                print "else"
                print upd_row
                self.cursor.execute(self.insert_query, (arg1, arg2, "Update Successful", str(datetime.now())))
                self.cnx.commit()
        else:
            self.cursor.execute(self.insert_query, (arg1, arg2, "Resource Not Present In Echo_Resource Table", \
                                        str(datetime.now())))
            self.cnx.commit()
    except Exception, e:
        print("Error")

def main():
echo_resource_obj = echoResource()
echo_resource_obj.connect()
echo_resource_obj.updateResource()

if __name__ == '__main__':
main()

Upvotes: 1

user4913191
user4913191

Reputation:

Updated the code and created an object to call the methods.

import MySQLdb
import logging
from opswareConnect import data
from echoResourceConfig import host, user, passwd, db, select_query, 
update_query, insert_query

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class echoResource(object):
def __init__(self):
    self.host = host
    self.user = user
    self.passwd = passwd
    self.db = db
    self.select_query = select_query
    self.update_query = update_query
    self.insert_query = insert_query


def connect(self):
    print "inside connect"
    try:
        self.cnx = MySQLdb.connect(host, user, passwd, db)
        if self.cnx:
            print("connection successful" , self.cnx)
        self.cursor = self.cnx.cursor()
    except Exception, e:
        print "Error"
    return self.cursor

def updateResource(self):
    try:
        for row in data:
            arg1 = [row["system_name"],]
            arg2 = [row["fdc_inv_sa_team"]]
        row = cursor.execute(select_query, arg1)
        print row
        if row:
            data = cursor.fetchall()
            upd_row = cursor.execute(update_query, (arg2, str(datetime.now()), arg1))

            if not upd_row:
                print "upd_row", upd_row
                cursor.execute(insert_query, (arg1, arg2, "Update Failed", str(datetime.now())))
                cnx.commit()
            else:
                print "else"
                print upd_row
                cursor.execute(insert_query, (arg1, arg2, "Update Successful", str(datetime.now())))
                cnx.commit()
        else:
            cursor.execute(insert_query, (arg1, arg2, "Resource Not Present In Echo_Resource Table", \
                                        str(datetime.now())))
            cnx.commit()
    except Exception, e:
        print("Error")

def main():
echo_resource_obj = echoResource()
echo_resource_obj.connect() 
echo_resource_obj.updateResource()

if __name__ == '__main__':
main()

Connection is successful but the code is throwing an exception and not executing the queries.

Upvotes: 0

stovfl
stovfl

Reputation: 15523

Question: Class does not print any statement while connecting

You are missing the self. prefix.
change

self.cnx = MySQLdb.connect(host, user, passwd, db)
    if cnx:

to

self.cnx = MySQLdb.connect(host, user, passwd, db)
    if self.cnx:

Upvotes: 0

Related Questions