Exemplo n.º 1
0
 def group_by(self, table, condition, group_by_fields, fields, page, count):
     where = self.dict2cond(condition)
     select_fields = ','.join(fields)
     group_fields = self.list2fields(group_by_fields)
     skip = (page - 1) * count
     if condition:
         sql_str = 'select {select_fields} from {table} where {condition} '\
             'group by {group_fields} limit {start},{count}'.format(
                         select_fields=select_fields,
                         table=table,
                         condition=where,
                         group_fields=group_fields,
                         start=skip,
                         count=count)
     else:
         sql_str = 'select {select_fields} from {table} '\
             'group by {group_fields} limit {start},{count}'.format(
                         select_fields=select_fields,
                         table=table,
                         group_fields=group_fields,
                         start=skip,
                         count=count)
     LOGGER.info(sql_str)
     try:
         ret = self.execute(sql_str)
         if not ret:
             return []
         return self.cur.fetchall()
     except Exception as e:
         LOGGER.error("ex: %s", e, exc_info=True)
         return []
Exemplo n.º 2
0
 def insert(self, table, data, update=True):
     SQL_FORMAT = "insert into `%s` (%s) values (%s)"
     keys = data.keys()
     values = data.values()
     field_str = self.list2fields(keys)
     value_str = self.list2values(values)
     sql_str = SQL_FORMAT % (table, field_str, value_str)
     LOGGER.info("insert: %s" % sql_str)
     try:
         ret = self.execute(sql_str, update)
         if not ret:
             return False, "unknow Error"
         if update:
             self.con.commit()
         return ret, "OK"
     except Exception as e:
         LOGGER.error("ex: %s", e, exc_info=True)
         return False, e