import MySQLdb
import sys
from libdesklets.controls import Control
from IDBConnection import IDBConnection
class DBConnection(Control, IDBConnection):
host = 'xxxx'
user = 'xxxx'
passwd = 'xxxx'
db = 'xxxx'
def __init__(self):
Control.__init__(self)
pass
def __get_dbconnection(self):
db = MySQLdb.connect(self.host, self.user, self.passwd, self.db)
return db
def __insert(self):
db = self.__get_dbconnection()
cursor = db.cursor()
cursor.execute("INSERT INTO Usernotes (UID, NID, Inhalt) VALUES (3, 1, 'text');")
cursor.close()
db.close()
def __select(self):
db = self.__get_dbconnection()
cursor = db.cursor()
cursor.execute("SELECT Inhalt FROM Usernotes WHERE UID = 1 AND NID = 1;")
cursor.close()
db.close()
def __update(self):
db = self.__get_dbconnection()
cursor = db.cursor()
cursor.execute("UPDATE Usernotes SET Inhalt = 'inserttest' WHERE UID = 1 AND NID = 2;")
cursor.close()
db.close()
insert = property(__insert, doc="insert into database")
select = property(__select, doc="select from database")
update = property(__update, doc="update database")
def get_class(): return DBConnection
上面的代码是一个用于 Linux gdesklets 的 mysql-Database 的控件(这就是 import Control 和 import IDBConnection 的来源)。因此,当我们从另一个文件 (dbc.insert() / dbc.select() / dbc.update()) 调用属性时,我们会收到错误“'NoneType' object is not callable”。如果我们添加返回类型,我们会得到“'ReturnType' 对象不可调用”。函数正在工作并且数据库操作已完成,但显示文件(调用函数的位置)在异常后崩溃。
希望有人可以在这里帮助我们。