実行文がクラスでラップされているときにPythonのtry/elseブロックでMySQLの警告を受け取ることができません。Trappingsクラスでラップされた呼び出しに関するMySQLの警告 - Python
私は属性としてMySQL接続オブジェクトとしてクラスを持ち、別のものとしてMySQLカーソルオブジェクトを持ち、そのカーソルオブジェクトを通してクエリを実行するメソッドを持っています。カーソル自体はクラスにラップされています。これらはクエリを正しく実行するようですが、生成されるMySQLの警告はtry/elseブロックで例外として捕捉されません。 try/elseブロックが警告をキャッチしないのはなぜですか?どのようにクラスやメソッドの呼び出しを修正して警告をキャッチするのですか?
また、私は著名な情報源を見てきましたが、これを理解するのに役立つディスカッションは見つかりませんでした。私はこれを説明する任意の参考に感謝します。
下記のコードをご覧ください。冗談のためのお詫び、私は初心者です。一見
#!/usr/bin/python
import MySQLdb
import sys
import copy
sys.path.append('../../config')
import credentials as C# local module with dbase connection credentials
#=============================================================================
# CLASSES
#------------------------------------------------------------------------
class dbMySQL_Connection:
def __init__(self, db_server, db_user, db_passwd):
self.conn = MySQLdb.connect(db_server, db_user, db_passwd)
def getCursor(self, dict_flag=True):
self.dbMySQL_Cursor = dbMySQL_Cursor(self.conn, dict_flag)
return self.dbMySQL_Cursor
def runQuery(self, qryStr, dict_flag=True):
qry_res = runQueryNoCursor(qryStr=qryStr, \
conn=self, \
dict_flag=dict_flag)
return qry_res
#------------------------------------------------------------------------
class dbMySQL_Cursor:
def __init__(self, conn, dict_flag=True):
if dict_flag:
dbMySQL_Cursor = conn.cursor(MySQLdb.cursors.DictCursor)
else:
dbMySQL_Cursor = conn.cursor()
self.dbMySQL_Cursor = dbMySQL_Cursor
def closeCursor(self):
self.dbMySQL_Cursor.close()
#=============================================================================
# QUERY FUNCTIONS
#------------------------------------------------------------------------------
def runQueryNoCursor(qryStr, conn, dict_flag=True):
dbMySQL_Cursor = conn.getCursor(dict_flag)
qry_res =runQueryFnc(qryStr, dbMySQL_Cursor.dbMySQL_Cursor)
dbMySQL_Cursor.closeCursor()
return qry_res
#------------------------------------------------------------------------------
def runQueryFnc(qryStr, dbMySQL_Cursor):
qry_res = {}
qry_res['rows'] = dbMySQL_Cursor.execute(qryStr)
qry_res['result'] = copy.deepcopy(dbMySQL_Cursor.fetchall())
qry_res['messages'] = copy.deepcopy(dbMySQL_Cursor.messages)
qry_res['query_str'] = qryStr
return qry_res
#=============================================================================
# USAGES
qry = 'DROP DATABASE IF EXISTS database_of_armaments'
dbConn = dbMySQL_Connection(**c.creds)
def dbConnRunQuery():
# Does not trap an exception; warning displayed to standard error.
try:
dbConn.runQuery(qry)
except:
print "dbConn.runQuery() caught an exception."
def dbConnCursorExecute():
# Does not trap an exception; warning displayed to standard error.
dbConn.getCursor() # try/except block does catches error without this
try:
dbConn.dbMySQL_Cursor.dbMySQL_Cursor.execute(qry)
except Exception, e:
print "dbConn.dbMySQL_Cursor.execute() caught an exception."
print repr(e)
def funcRunQueryNoCursor():
# Does not trap an exception; no warning displayed
try:
res = runQueryNoCursor(qry, dbConn)
print 'Try worked. %s' % res
except Exception, e:
print "funcRunQueryNoCursor() caught an exception."
print repr(e)
#=============================================================================
if __name__ == '__main__':
print '\n'
print 'EXAMPLE -- dbConnRunQuery()'
dbConnRunQuery()
print '\n'
print 'EXAMPLE -- dbConnCursorExecute()'
dbConnCursorExecute()
print '\n'
print 'EXAMPLE -- funcRunQueryNoCursor()'
funcRunQueryNoCursor()
print '\n'
あなたは何を期待していますか?問題を示す最小の例にコードを縮小できますか? –
また、なぜこれはとても複雑ですか?何を最適化しようとしていますか?なぜグローバル関数を呼び出すクラスを持っていますか? –
どのような警告が表示されますか? *警告*は必ず例外的なものとは限りません。 –