2
由于某种原因,每当我在db游标上运行查询时,它在.messages列表中会生成两个错误,这是一个特性吗?python mysqldb游标消息列表显示错误两次
这里是运行查询的代码,所有的应用程序是开放的数据库连接,一旦与强制错误或许运行此,请阅读.messages,然后退出
import MySQLdb
class dbobject:
def __init__(self, dbhost, dbuser, dbpass, dbname):
self.connection = MySQLdb.connect(user=dbuser, passwd=dbpass, host=dbhost, db=dbname)
self.cursor = self.connection.cursor()
def try_query(self, query, args=()):
""" attempts to run a query, where
query is the query to be run, with '%s' statements where the values should be, and
args is a tuple of the values to go with the query"""
try:
if args ==():
self.cursor.execute(query)
else:
self.cursor.execute(query, args)
self.connection.commit()
except:
self.connection.rollback()
def add_unit(self, name, version, credits):
""" takes name, version, credits
name is the name of the unit paper
version is the unit version, and
credits is how many credits its worth"""
self.try_query("insert into dsfdf tbl_unit (unit_name, unit_version, unit_credits) values (%s,%s,%s)",(name,version,credits))
def close(self):
self.cursor.close()
self.connection.close()
blah = dbobject(#####################)
blah.add_unit("thing", "something", 6)
for i in blah.cursor.messages:
print i
blah.close()
谢谢,最终被试图让不正确的查询错误日志,并且使用异常中的信息代替,应该早一点想到 – biokiwi 2010-07-24 06:25:20