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"])