I'm learning how to program and handle Python classes and have found this problem.
I have this files (example.py) and inside this file I'm importing two classes.
example.py
..
from class_folder.automobile import Class_Automobile
from class_folder.catalog import Class_Catalog
class_automobile = Class_Automobile()
class_catalog = Class_Catalog()
..
Inside automobile.py have this code:
from pdo_mysql import Pdo_Mysql
class Class_Automobile(object):
"""
Description
"""
__pdo_mysql_intern = None
def __init__(self):
self.__pdo_mysql_intern = Pdo_Mysql('host', 'user', 'password', 'db')
## End def init
def Validate_Make(self, make_search):
query_make = 'make_text = %s '
result = self.__pdo_mysql_intern.select('make', query_make, 'id_make', make=make_search)
return result
## End function Validate Make
And inside the catalog.py file has this information:
from pdo_mysql import Pdo_Mysql
class Class_Catalog(object):
"""
Description
"""
__pdo_mysql_intern = None
def __init__(self):
self.__pdo_mysql_intern = Pdo_Mysql('host', 'user', 'password', 'db2')
## End def init
And finally the pdo_mysql.py file has this:
import MySQLdb, sys
class Pdo_Mysql(object):
"""
Description
"""
__instance = None
__host = None
__user = None
__password = None
__database = None
__session = None
__connection = None
def __new__(cls, *args, **kwargs):
if not cls.__instance:
cls.__instance = super(Pdo_Mysql, cls).__new__(cls,*args,**kwargs)
return cls.__instance
## End __new__
def __init__(self, host='localhost', user='root', password='', database=''):
self.__host = host
self.__user = user
self.__password = password
self.__database = database
## __init__
def _open(self):
try:
cnx = MySQLdb.connect(self.__host, self.__user, self.__password, self.__database)
self.__connection = cnx
self.__session = cnx.cursor()
except MySQLdb.Error as e:
print "Error %d: %s" % (e.args[0],e.args[1])
## End function open
def _close(self):
self.__session.close()
self.__connection.close()
## End function close
def select(self, table, where=None, *args, **kwargs):
result = None
query = 'SELECT '
keys = args
values = tuple(kwargs.values())
l = len(keys) - 1
for i, key in enumerate(keys):
query += "`"+key+"`"
if i < l:
query += ","
## End for keys
query += 'FROM %s' % table
if where:
query += " WHERE %s" % where
## End if where
self._open()
self.__session.execute(query, values)
result = [item[0] for item in self.__session.fetchall()]
self._close()
return result
## End function select
When I run the example.py I have this problem, somehow when I call this
validate_information = class_automobile.Validate_Make(make)
I obtain this response
File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1146, "Table 'db2.make' doesn't exist")
This problem it is because the db from Class_Catalog is mixing when I'm calling the Class_Automobile. It is solved when I put the Class_Catalog before the Class_Automobile but I want to know how to solve properlly.
Update:
Thanks to Adam Smith found the problem. I just had to change this file pdo_mysql.py:
class Pdo_Mysql(object):
# all your existing definitions
def __new__(cls, *args, **kwargs):
**if not cls.__instance or not cls.__database:**
cls.__instance = super(Pdo_Mysql, cls).__new__(cls,*args,**kwargs)
return cls.__instance
Validate_Make()as well in theClass_Automobile()? 2). What is the argumentmakethat you are passing into that function? 3). Does that parameter coverdb2database as well?