Python使用来自超类的mysql连接
下面只是一个示例代码,它提供了一个错误,我希望能得到修复方面的帮助,或者了解编写此错误的更好方法。我有一个名为mysql\u connection的mysql“super”类。在这个类中,建立到数据库的连接。我也有一些方法。一个简单地运行“selectversion()”以显示连接/查询工作的程序。然后我有一个“chktable”方法,在本例中,该方法实例化了一个名为“table”的新子类,该子类继承了超类。在实例化该类之后,我调用子类中的一个方法,该方法尝试使用超类中的查询方法来运行“show tables like'tbl name'”。这就是我出错的地方Python使用来自超类的mysql连接,python,mysql,mysql-connector-python,Python,Mysql,Mysql Connector Python,下面只是一个示例代码,它提供了一个错误,我希望能得到修复方面的帮助,或者了解编写此错误的更好方法。我有一个名为mysql\u connection的mysql“super”类。在这个类中,建立到数据库的连接。我也有一些方法。一个简单地运行“selectversion()”以显示连接/查询工作的程序。然后我有一个“chktable”方法,在本例中,该方法实例化了一个名为“table”的新子类,该子类继承了超类。在实例化该类之后,我调用子类中的一个方法,该方法尝试使用超类中的查询方法来运行“show
import mysql.connector
from mysql.connector import errorcode
from 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 as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print "Something is wrong with your user name or password"
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print "Database does 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 like '{}".format(self.name))
conn = mysql_connection()
conn.get_version()
conn.chktable("test")
我得到的错误是:
$ 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 like '{}".format(self.name))
File "example.py", line 28, in query
cursor = MySQLCursor(self.cnx)
AttributeError: 'table' object has no attribute 'cnx'
$python example.py
[(u'5.1.73',)]
回溯(最近一次呼叫最后一次):
文件“example.py”,第50行,在
连接表(“测试”)
chktable中第39行的文件“example.py”
tab.检查表()
检查表中第46行的文件“example.py”
返回super(table,self).query(“显示类似{}.format(self.name)的表”)
查询中第28行的文件“example.py”
cursor=MySQLCursor(self.cnx)
AttributeError:“table”对象没有属性“cnx”
我不完全理解调用超类以及它如何与子类一起工作,所以这可能是我的问题。我也想知道是否有更好的方法来实现这一点。我想我可以完全摆脱这个子类,但是我喜欢我创建的子类,所以我觉得应该有办法解决它。我可以尝试的第二件事是将子类放在主类中,但我认为这是不对的。正如Jon指出的,这不是继承的适当用法。这就是“是一种”关系:狗从动物身上继承下来,因为狗是一种动物。但表不是连接:表可能使用连接,但这仅仅意味着您应该将连接的实例分配给表中的实例变量 此外,在继承关系中,通常没有很好的理由让超类知道它的子类,正如您在
chktable
方法中所做的那样
(您看到的实际错误是因为您没有在表的
\uuuu init\uuuu
中调用超类方法,但最好修复您的结构。)为什么您的表从连接继承?!非常感谢。您是对的,它不是继承的适当使用。虽然这只是一个示例,mysql_Connection类在实际代码中的名称也有所不同,但我的类是一个“has a”组合,而不是一个“is a”。进一步思考我的代码后,我意识到我不会在其他任何地方使用“has a”类,所以我将把它添加到主类中。我可能会制作一个实际的mysql连接组合类,将其与我正在处理的主要mysql类分离。非常好地解释了何时使用继承以及如何在不使用和实例变量的情况下处理这种情况