I am accessing a MySQL database from python via MySQLdb library. I am attempting to test the database connection as shown below.
我正在通过MySQLdb库从python访问MySQL数据库。我正在尝试测试数据库连接,如下所示。
db = MySQLdb.connect(self.server, self.user,
self.passwd, self.schema)
cursor = db.cursor()
try:
cursor.execute("SELECT VERSION()")
results = cursor.fetchone()
ver = results[0]
if (ver is None):
return False
else:
return True
except:
print "ERROR IN CONNECTION"
return False
Is this the right way one should test the connectivity when writing unit testcases? If there is a better way, please enlighten!
在编写单元测试用例时,是否应该使用这种方法来测试连接性?如果有更好的办法,请启发!
2 个解决方案
#1
8
I could be wrong (or misinterpreting your question :) ), but I believe the a connection-related exception gets thrown on MySQLdb.connect()
. With MySQLdb, the exception to catch is MySQLdb.Error
. Therefore, I would suggest moving the db
setup inside of the try
block and catching the proper exception (MySQLdb.Error
). Also, as @JohnMee mentions, fetchone()
will return None if there are no results, so this should work:
我可能是错的(或者误解了您的问题:),但是我相信在MySQLdb.connect()上抛出了一个与连接相关的异常。对于MySQLdb,要捕获的异常是MySQLdb. error。因此,我建议将db设置移动到try块中并捕获适当的异常(MySQLdb.Error)。而且,正如@JohnMee提到的,如果没有结果,fetchone()将不会返回任何结果,所以这应该是可行的:
try:
db = MySQLdb.connect(self.server, self.user,
self.passwd, self.schema)
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
results = cursor.fetchone()
# Check if anything at all is returned
if results:
return True
else:
return False
except MySQLdb.Error:
print "ERROR IN CONNECTION"
return False
If you don't care about the connection and are just looking to test the execution of the query, I guess you could leave the connection setup outside the try
but include MySQLdb.Error
in your exception, perhaps as follows:
如果您不关心连接,而只想测试查询的执行,我想您可以将连接设置放在try之外,但包含MySQLdb。您的异常错误,可能如下:
db = MySQLdb.connect(self.server, self.user,
self.passwd, self.schema)
cursor = db.cursor()
try:
cursor.execute("SELECT VERSION()")
results = cursor.fetchone()
# Check if anything at all is returned
if results:
return True
else:
return False
except MySQLdb.Error, e:
print "ERROR %d IN CONNECTION: %s" % (e.args[0], e.args[1])
return False
This would at least give you a more detailed reason of why it failed.
这至少会给你一个更详细的理由说明为什么它失败了。
#2
2
Yes. Looks good to me.
是的。我看起来很好。
My personal preferences:
我的个人喜好:
- actually throw an exception if no connection
- 如果没有连接,则抛出异常。
- you only need to fetchone, the test for None is superfluous (unless you're keen to enforce a minimum version of the database)
- 您只需要fetchone,对None的测试是多余的(除非您热衷于执行数据库的最小版本)
#1
8
I could be wrong (or misinterpreting your question :) ), but I believe the a connection-related exception gets thrown on MySQLdb.connect()
. With MySQLdb, the exception to catch is MySQLdb.Error
. Therefore, I would suggest moving the db
setup inside of the try
block and catching the proper exception (MySQLdb.Error
). Also, as @JohnMee mentions, fetchone()
will return None if there are no results, so this should work:
我可能是错的(或者误解了您的问题:),但是我相信在MySQLdb.connect()上抛出了一个与连接相关的异常。对于MySQLdb,要捕获的异常是MySQLdb. error。因此,我建议将db设置移动到try块中并捕获适当的异常(MySQLdb.Error)。而且,正如@JohnMee提到的,如果没有结果,fetchone()将不会返回任何结果,所以这应该是可行的:
try:
db = MySQLdb.connect(self.server, self.user,
self.passwd, self.schema)
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
results = cursor.fetchone()
# Check if anything at all is returned
if results:
return True
else:
return False
except MySQLdb.Error:
print "ERROR IN CONNECTION"
return False
If you don't care about the connection and are just looking to test the execution of the query, I guess you could leave the connection setup outside the try
but include MySQLdb.Error
in your exception, perhaps as follows:
如果您不关心连接,而只想测试查询的执行,我想您可以将连接设置放在try之外,但包含MySQLdb。您的异常错误,可能如下:
db = MySQLdb.connect(self.server, self.user,
self.passwd, self.schema)
cursor = db.cursor()
try:
cursor.execute("SELECT VERSION()")
results = cursor.fetchone()
# Check if anything at all is returned
if results:
return True
else:
return False
except MySQLdb.Error, e:
print "ERROR %d IN CONNECTION: %s" % (e.args[0], e.args[1])
return False
This would at least give you a more detailed reason of why it failed.
这至少会给你一个更详细的理由说明为什么它失败了。
#2
2
Yes. Looks good to me.
是的。我看起来很好。
My personal preferences:
我的个人喜好:
- actually throw an exception if no connection
- 如果没有连接,则抛出异常。
- you only need to fetchone, the test for None is superfluous (unless you're keen to enforce a minimum version of the database)
- 您只需要fetchone,对None的测试是多余的(除非您热衷于执行数据库的最小版本)