所以MainApp.py中的一些代码就像这样,我关注的是DBQuery发生的地方。 DB302.py与MainApp.py位于同一文件夹中,并包含一个名为DB302的类,其成员方法查询如下所示。为什么它仅在我使用DB302.DB302.query(DBQuery...)
DBQuery
DB302.DB302...
已经是DBQuery = DB302.DB302()
的实例时才有效?有没有办法不必首先使用,实例DBQuery = DB302
而不只是{ {1}}并且还能够将它用作`DBQuery.query(Instance,var1,var2)。
我的第二个问题涉及在DB302.py中使用self.method or variable
。我是否正确使用过它们?我现在正在ThrowsU()
出现,并且无法弄清楚原因。我的数据库已设置为正常工作,我可以通过python终端访问它。 Id是主键,我为每个测试用例递增它。
#MainApp.py
import DB302
class MainApp(object):
@cherrypy.expose
def default(self, *args, **kwargs):
page = "<b>"args" has %d variables</b><br/>\n" % len(args)
for x in xrange(0, len(args)):
if args[x] == "":
page += "<i>error</i>\n"
else:
page += args[x] + "<br/>\n"
page += "<b>"cherrypy.request.params" has %d variablez</b><br/>\n" % len(cherrypy.request.params)
for key, value in cherrypy.request.params.items():
if cherrypy.request.params[key] == "":
page += "<i>empty</i>\n"
elif key == "username":
DBQuery = DB302.DB302()
data = DB302.DB302.query(DBQuery, 'SELECT * FROM test', 'SELECT')
else:
page += key + " = " + value + "<br/>\n"
page += """
<form name="form1" method="POST" action="/testURL/part2">
<input name="username" type="text" maxlength="256" autocomplete="off">
</input>
</form>
"""
return page
#DB302.py
import sqlite3
class DB302(object):
def __init__(self):
print "<b>Within DB302</b><br/>\n"
def throws(self):
raise RuntimeError('Connection to DB failed, terminating...')
def throwsQ(self):
raise RuntimeError('Error in DB query, terminating...')
def throwsU(self):
raise RuntimeError('Unknown DB error encountered...')
def connect(self):
# Where the DB connection will be made
CS302="<!-- Private -->" # The datbase file is on my desktop
try:
DBCon=sqlite3.connect(CS302)
cursor=DBCon.cursor()
return True
except IOError:
self.throws()
except:
self.throwsU()
else:
return False
def disconnect(self):
# Used to disconnect...
try:
self.cursor.close()
return True
except IOError:
self.throws()
except:
self.throwsU()
else:
return False
def query(self, queryString, queryType):
# Queries will be made here
if self.connect():
try:
self.cursor.execute(queryString)
if queryType == "SELECT":
# return all the rows if there's a select query
allRows = self.cursor.fetchall()
return allRows
elif queryType == "DELETE":
# return affected rows if there's a deletion
rowsAffected = self.cursor.rowcount()
self.DBCon.commit()
return rowsAffected
elif queryType == "INSERT":
# return true if success
self.DBCon.commit()
return True
elif queryType == "CREATE":
self.DBCon.commit()
return True
else:
return False
except IOError:
self.throwsQ()
except:
self.throwsU()
else:
return False
答案 0 :(得分:1)
为了让您更轻松地调用您的类方法:
from DB302 import DB302 as DBQuery
...
DBQuery.query(Arg1, Arg2)
对于你的第二个问题:
在可能的情况下,您应该避免使用所谓的“裸”except
,即except:
这种令人愉快的结构具有令人遗憾的特性,即使调试变得更加困难。
所以我建议改为采纳这里的建议:http://wiki.python.org/moin/HandlingExceptions
并以打印出异常信息的方式捕获所有异常:
import sys
...
except:
e = sys.exc_info()[1]
print e
我还注意到您的自定义例外不会从Exception
继承
这个问题和随附的答案可能提供一些见解:Proper way to declare custom exceptions in modern Python?