Python Using mysql connection from super class -


below example code provides error hoping on fixing, or getting understanding of better way write this. have mysql "super" class called mysql_connection. in class, connection database made. have few methods within it. 1 runs "select version()" show connection/query works. have "chktable" method in example instantiates new subclass called "table" inherits super class. after instantiating class, call method within subclass attempts use the query method in superclass run "show tables 'tbl name'". error.

import mysql.connector mysql.connector import errorcode mysql.connector.cursor import mysqlcursor  class mysql_connection(object):     def __init__(self, **kwargs):         self.connection_options = {}         self.connection_options['user'] = 'root'         self.connection_options['password'] = ''         self.connection_options['host'] = '192.168.33.10'         self.connection_options['port'] = '3306'         self.connection_options['database'] = "test"         self.connection_options['raise_on_warnings'] = true         self.connect()      def connect(self):         try:             self.cnx = mysql.connector.connect(**self.connection_options)         except mysql.connector.error err:             if err.errno == errorcode.er_access_denied_error:                 print "something wrong user name or password"             elif err.errno == errorcode.er_bad_db_error:                 print "database not exists"              else:                 print err      def query(self, statement, data=''):         cursor = mysqlcursor(self.cnx)         cursor.execute(statement)         result = cursor.fetchall()         cursor.close         return result      def get_version(self):         print self.query("select version()")      def chktable(self, tb_name):         tab = table(name=tb_name)         tab.check_table()  class table(mysql_connection):     def __init__(self, **kwargs):         self.name = kwargs['name']      def check_table(self):         return super(table, self).query("show tables '{}".format(self.name))  conn = mysql_connection() conn.get_version() conn.chktable("test") 

the error is:

$ python example.py [(u'5.1.73',)] traceback (most recent call last):   file "example.py", line 50, in <module>     conn.chktable("test")   file "example.py", line 39, in chktable     tab.check_table()   file "example.py", line 46, in check_table     return super(table, self).query("show tables '{}".format(self.name))   file "example.py", line 28, in query     cursor = mysqlcursor(self.cnx)     attributeerror: 'table' object has no attribute 'cnx' 

i don't understand calling super class , how works subclasses, issue. i'd know if there maybe better way accomplish this. thinking rid of subclass altogether, subclass made feel there should way around it. secondary thing try put subclass inside master class, don't think correct.

as jon points out, not appropriate use of inheritance. "is-a" relationships: ie dog inherits animal, because dog animal. table not connection: table might use connection, means should assign instance of connection instance variable in table.

also, in inheritance relationship there no reason superclass know subclasses, have in chktable method.

(the actual bug you're seeing because haven't called superclass method in table's __init__, it's better fix structure.)


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -