def sql_query_to_dct(sql): db = model_connect.getdb() if None == db: logger.error('get db connect error.') return (False, {}) logger.debug('get db connect success.') res = {} try: logger.debug('sql: %s' % sql) #rs = db.query(sql.encode('utf8')) if isinstance(sql, unicode): sql = sql.encode('utf8') rs = db.query(sql) for rd in rs: key = rd[0] value = rd[1] if isinstance(key, unicode): #logger.debug('do encode utf8 raw_key: %s' % key) key = key.encode('utf8') #logger.debug('res key: %s' % key) if isinstance(value, unicode): value = value.encode('utf8') if key != key.strip(): logger.warning('key has Illegal character space. key:%s' % key) res[key.strip()] = value except: logger.error('db query failed, error: %s' % str(sys.exc_info())) return (False, {}) return (True, res)
def Get(day_index): sql = "select day_index, db_update_time, status, try_times, cache_update_time from offline_update_status where day_index = '%s'" % day_index db = model_connect.getdb() if None == db: logger.error('get db connect error.') raise RuntimeError('get db connect error.') try: rs = db.query(sql) if 0 == len(rs): return None ocs = OfflineCacheStatus() rd = rs[0] ocs.day_index = rd[0] ocs.db_update_time = rd[1] ocs.status = rd[2] ocs.try_times = rd[3] if None == ocs.try_times: ocs.try_times = 0 ocs.cache_update_time = rd[4] return ocs except: logger.error('OfflineCacheStatus.load failed. error:%s' % str(sys.exc_info())) print str(sys.exc_info()) raise RuntimeError('db query error.') return None
def save(self): sql_list = [] if not OfflineCacheStatus.exists(self.day_index): sql = "insert into offline_update_status(day_index) values('%s')" % self.day_index sql_list.append(sql) sql = "update offline_update_status set " if None != self.db_update_time: sql += "db_update_time = '%s'," % date_to_string(self.db_update_time) if '' != self.status: sql += "status = '%s'," % self.status sql += "try_times = %s," % self.try_times if None != self.cache_update_time: sql += "cache_update_time = '%s'," % date_to_string(self.cache_update_time) sql = sql[:-1] + " where day_index='%s'" % self.day_index sql_list.append(sql) db = model_connect.getdb() if None == db: logger.error('get db connect error.') raise RuntimeError('get db connect error.') try: for sql in sql_list: #print 'sql: %s' % sql if False == db.executeEx(sql): logger.error('do db execute error. sql:%s' % sql) return False return True except: logger.error('OfflineCacheStatus.load failed. error:%s' % str(sys.exc_info())) print str(sys.exc_info()) raise RuntimeError('db query error.')
def do_db_excute(sql): db = model_connect.getdb() if None == db: logger.error('get db connect error.') return False logger.debug('get db connect success.') try: if isinstance(sql, unicode): sql = sql.encode('utf8') logger.debug('sql: %s' % sql) return db.executeEx(sql) except: logger.error('db insert data failed, error: %s' % str(sys.exc_info())) return False
def exists(day_index): sql = "select * from offline_update_status where day_index = '%s'" % day_index db = model_connect.getdb() if None == db: logger.error('get db connect error.') raise RuntimeError('get db connect error.') try: rs = db.query(sql) if 0 == len(rs): return False return True except: logger.error('OfflineCacheStatus.exists failed. error:%s' % str(sys.exc_info())) print str(sys.exc_info()) raise RuntimeError('db query error.')
def delete(self): if not OfflineCacheStatus.exists(self.day_index): return True sql = "delete from offline_update_status where day_index='%s'" % self.day_index db = model_connect.getdb() if None == db: logger.error('get db connect error.') raise RuntimeError('get db connect error.') try: if False == db.executeEx(sql): logger.error('do db execute error. sql:%s' % sql) return False return True except: logger.error('OfflineCacheStatus.delete failed. error:%s' % str(sys.exc_info())) print str(sys.exc_info()) raise RuntimeError('db query error.')