def read_data(self, table_name, field=None, **kw): """ 一个简易的数据读取接口 :param table_name: :param field: :param kw: :return: """ try: cursor = BaseModel(table_name, self.location, self.dbname).query(kw, field) data = pd.DataFrame() if cursor.count(): data = pd.DataFrame(list(cursor)) except Exception as e: ExceptionInfo(e) finally: cursor.close() return data
def aggregate(self, table_name, pipeline): """ :param table_name: :param pipeline: :return: """ try: cursor = BaseModel(table_name, self.location, self.dbname).aggregate(pipeline) # data = pd.DataFrame() # if cursor.count(): data = pd.DataFrame(list(cursor)) except Exception as e: ExceptionInfo(e) finally: cursor.close() return data
def main(): model = BaseModel() print('select----start') res = model.select('SELECT * FROM weather;') print(res) model.execute( 'INSERT INTO weather (city, temp_lo, temp_hi, prcp, date) VALUES (\'a\', 1, 1, 3.5, \'1961-06-16\');' ) model.execute( 'INSERT INTO weather (city, temp_lo, temp_hi, prcp, date) VALUES (\'b\', 2, 2, 3.5, \'1961-06-16\');' ) model.execute('UPDATE weather SET temp_lo=20 WHERE temp_hi=2;') model.execute('DELETE FROM weather WHERE temp_lo=20;') print('select----end') res = model.select('SELECT * FROM weather;') print(res) model.close()
def min(self, table_name, field='_id', **kw): """ 找到满足kw条件的field列上的最小值 :param table_name: :param field: :param kw: :return: """ try: if not isinstance(field, str): raise TypeError('field must be an instance of str') cursor = BaseModel(table_name, self.location, self.dbname).query(sql=kw, field={field: True}) if cursor.count(): d = pd.DataFrame(list(cursor)) m = d.loc[:, [field]].min()[field] else: m = None cursor.close() return m except Exception as e: raise e