def __init__(self): self.__connection = SQLiteUtil().Connect('todolist') self.__cur = self.__connection.cursor()
class todolist(object): def __init__(self): self.__connection = SQLiteUtil().Connect('todolist') self.__cur = self.__connection.cursor() @staticmethod def mapRecord2TodoItem(row): return TodoItem(row[0], row[1], row[2], row[3], row[4]) def getTodoLists(self): sql = '''select todoId, title, taskContent, warningDate, status from todolist order by todoId desc''' self.__cur.execute(sql) rs = self.__cur.fetchall() self.__connection.commit() return list(map(self.mapRecord2TodoItem, rs)) def getTodo(self, id): sql = '''select todoId, title, taskContent, warningDate, status from todolist where todoId = %s''' % id self.__cur.execute(sql) rs = self.__cur.fetchall() self.__connection.commit() if(0 == len(rs)): return None else: return self.mapRecord2TodoItem(rs[0]) def addTodo(self, title, content): sql = '''insert into todolist(title, taskContent, warningDate, status) values(?, ?, ?, 0)''' warningDate = datetime.datetime.now() self.__cur.execute(sql, (title, content, warningDate)) self.__cur.execute('select last_insert_rowid() newid') rs = self.__cur.fetchall() self.__connection.commit() return TodoItem(rs[0][0], title, content, warningDate, 0) def delTodo(self, id): sql = 'delete from todolist where todoid = %s' % id self.__cur.execute(sql) self.__connection.commit() def updateTodo(self, todo): if(type(todo) == TodoItem): updtdi = todo oldtdi = self.getTodo(updtdi.TodoID) isUpdate = False sql = 'update todolist set' if(updtdi.Title != oldtdi.Title): sql = sql + ' Title="%s"' % updtdi.Title isUpdate = True if(updtdi.Content != oldtdi.Content): sql = sql + ' taskContent="%s"' % updtdi.Content isUpdate = True if(updtdi.WarningDate != oldtdi.WarningDate): sql = sql + ' warningDate=%s' % updtdi.WarningDate isUpdate = True if(updtdi.Status != oldtdi.Status): sql = sql + ' status=%s' % 1 if(updtdi.Status) else 0 isUpdate = True if(isUpdate): self.__cur.execute(sql) self.__connection.commit()