def delete(cls, ids): state = False sql = 'delete from {} where id=%s'.format(cls.__table__) ids = ids if isinstance(ids, list) else [ids] try: for id in ids: params = (id,) store.execute(sql, params) store.commit() state = True except: store.rollback() return state
def update(cls, id, keys, values): keys = keys if isinstance(keys, list) else [keys] values = values if isinstance(values, list) else [values] keyformat = '=%s,'.join(keys) + '=%s' sql = 'update {} set {} where id=%s'.format(cls.__table__, keyformat) values.append(int(id)) params = tuple(values) rcnt = 0 print sql % params try: rcnt = store.execute(sql, params, True) store.commit() except: print 'except:', sql % params store.rollback() print 'rcnt=', rcnt return cls.get(id=id) if rcnt > 0 else None
def update(cls, id, content, news_id, author_id): sql = '''update {} set content=%s, news_id=%s, author_id=%s where id=%s'''.format(cls.__table__) params = (content, news_id, author_id, id) try: store.execute(sql, params) _id = store.commit() except e: print "Error", e.args[0], e.args[1] store.rollback() return cls.get(_id) if _id else None
def update(cls, id, name): sql = 'update {} set name=%s where id=%s'.format(cls.__table__) params = (name, id) try: store.execute(sql, params) _id = store.commit() except e: print "Error", e.args[0], e.args[1] store.rollback() return cls.get(_id) if _id else None
def create(cls, name): sql = 'insert into {}(name) values(%s)'.format(cls.__table__) params = (name, ) try: store.execute(sql, params) _id = store.commit() except e: print "Error", e.args[0], e.args[1] store.rollback() return cls.get(_id) if _id else None
def create(cls, content, news_id, author_id): sql = '''insert into {}(content, news_id, author_id, create_time) values(%s, %s, %s, %s) '''.format(cls.__table__) params = (content, news_id, author_id, now()) try: store.execute(sql, params) _id = store.commit() except e: print "Error", e.args[0], e.args[1] store.rollback() return cls.get(_id) if _id else None
def create(cls, title, content, cover_url, category_id, author_id, alias_title=None): sql = '''insert into {}(title, alias_title, content, cover_url, category_id, author_id, create_time) values(%s, %s, %s, %s, %s, %s, %s) '''.format(cls.__table__) params = (title, alias_title, content, cover_url, category_id, author_id, now()) try: store.execute(sql, params) _id = store.commit() except e: print "Error", e.args[0], e.args[1] store.rollback() print _id return cls.get(id=_id) if _id else None
def create(cls, username, passwd, nickname, gender, birthday, avatar_url): salt = ''.join(random.sample(string.ascii_letters, 6)) passwd = md5.new(passwd + salt).hexdigest() sql = '''insert into {}(username, passwd, nickname, salt, gender, birthday, avatar_url, register_time) values(%s, %s ,%s, %s, %s, %s, %s, %s)'''.format(cls.__table__) params = (username, passwd, nickname, salt, gender, birthday, avatar_url, now()) try: store.execute(sql, params) _id = store.commit() except e: print "Error", e.args[0], e.args[1] store.rollback() print _id return cls.get(_id) if _id else None