def delete(self,pk): sql = 'delete from %s where %s=%s' % (self.__table__, self.__primary_key__,pk) db = MySQLApp().getInstance() result = db.update(sql) ##删除操作也是更新 主要是游标 _cur db.close() print(result) return result
def get(self): # low content is to modify,only is to yinfu current_page = int(self.get_argument('current_page', 1)) eq_level_hight = str(self.get_argument('eq_level_hight', 10000)) eq_level_low = str(self.get_argument('eq_level_low', -10000)) eq_depth_hight = str(self.get_argument('eq_depth_hight', 10000)) eq_depth_low = str(self.get_argument('eq_depth_low', -10000)) response = {'status': 200, 'message': 'ok'} sql = "select * from earthquake where level >%s%s%s and level< %s%s%s and depth >%s%s%s and depth < %s%s%s"%\ ("\'", eq_level_low, "\'", "\'", eq_level_hight, "\'", "\'", eq_depth_low, "\'", "\'", eq_depth_hight, "\'") # sql = "select * from earthquake where level between (%s, %s) and depth between (%s, %s)"%\ # (eq_level_low, eq_level_hight,eq_depth_hight,eq_depth_low) print sql db = MySQLApp().getInstance() db.query(sql) # default each page count 10 result = db.fetchAllRows() earthquake_str = list() for earthquake in result: if earthquake.get('date'): earthquake_str.append(earthquake.update({'date': earthquake['date'].strftime('%Y-%m-%d %H:%M:%S')})) else: earthquake_str.append(earthquake) count_page = len(earthquake_str) data_list = earthquake_str[(current_page-1)*10:current_page*10] response.update({"data": data_list, 'count_page': count_page, 'current_page': current_page}) response = json.dumps(response) response = self.get_argument('jsoncallback')+"("+response+")" self.set_header("Access-Control-Allow-Origin", "*") self.write(response)
def save(self): fields = [] params = [] ##参考 注册 处理 程序 newUser for k, v in self.__mappings__.iteritems(): if v.canset == False or getattr( self, k, None) == None: ##如果不能被赋值,或则在表中没有这个字段,类中没有这个属性 continue fields.append(v.name) ##意思是:为属性集合增加元素,增加的元素是v.name,v=values值 vu = getattr(self, k, None) if isinstance(vu, unicode): vu = vu.encode('utf-8') if isinstance(vu, str) == False: vu = str(vu) params.append("'" + vu + "'") ## 意思是:为参数集合添加元素, 增加的元素是 vu sql = 'insert into %s (%s) values (%s)' % ( self.__table__, ','.join(fields), ','.join(params)) print('SQL: %s' % sql) print('ARGS: %s' % str(params)) db = MySQLApp().getInstance() pk = db.insert(sql) db.close() print(pk) return pk ##返回一个主键,也就是一个数字
def get(cls, pk):## 根据 关键字 查询这条记录 sql = 'select * from %s where %s=%s' % (cls.__table__, cls.__primary_key__,pk) db = MySQLApp().getInstance() db.query(sql) result = db.fetchOneRow() db.close() return cls(**result) if result else None ## 他的返回值是个什么???? 返回的是一个类带有及个参数
def get(self): response = {'status': 200, 'message': 'ok'} sql = "select * from earthquake " db = MySQLApp().getInstance() db.query(sql) # default each page count 10 result = db.fetchAllRows() earthquake_str = list() for earthquake in result: if earthquake.get('date'): earthquake_str.append(earthquake.update({'date': earthquake['date'].strftime('%Y-%m-%d %H:%M:%S')})) else: earthquake_str.append(earthquake) response.update({"data": earthquake_str}) response = json.dumps(response) response = self.get_argument('jsoncallback')+"("+response+")" self.set_header("Access-Control-Allow-Origin", "*") self.write(response)
def post(self, *args, **kwargs): username = self.get_argument('user', '') password = self.get_argument('pwd', '') code = self.get_argument('code', '') http_reponse = {'data': list(), 'message': 'ok', 'status': 200} sh = shelve.open('cookie_token.save') if code != sh.get('code'): http_reponse['message'] = 'verifycode is error' http_reponse['status'] = 1001 response = json.dumps(http_reponse) self.write(response) return None # query user db = MySQLApp().getInstance() sql = "select * from gly where DLM ='%s' and MM = '%s' " % ( username, password) db.query(sql) result = db.fetchAllRows()[0] if not result: http_reponse['message'] = 'user is not exist' response = json.dumps(http_reponse) self.write(response) return None USERID = str(result.get('id')) sh['user_id'] = USERID sh.close() # load data db = MySQLApp().getInstance() db.query(sql) # default each page count 10 result = db.fetchAllRows()[(1 - 1) * 10:1 * 10 - 1] http_reponse.update({'data': result}) response = http_reponse response = self.get_argument('jsoncallback') + "(" + json.dumps( response) + ")" self.set_header("Access-Control-Allow-Origin", "*") self.write(response)
def get(self): # sh = shelve.open('cookie_token.save') # if not sh.get('user_id'): # self.redirect('/login') user_name = self.get_argument('user_name', '') current_page = int(self.get_argument('current_page', 1)) sql = "select * from gly " if user_name: sql = "select * from gly where XM like %s%s%s" % ( "\'%", user_name, "%\'") db = MySQLApp().getInstance() db.query(sql) # default each page count 10 result = db.fetchAllRows() count_page = len(result) data_list = result[(current_page - 1) * 10:current_page * 10] earthquake_str = list() for earthquake in data_list: if earthquake.get('date'): earthquake_str.append( earthquake.update({ 'date': earthquake['date'].strftime('%Y-%m-%d %H:%M:%S') })) else: earthquake_str.append(earthquake) response = { "data": earthquake_str, 'count_data': count_page, 'current_page': current_page, 'status': 200, 'message': 'ok', "last_time": time.time() * 1000 } respon = json.dumps(response) response = self.get_argument('jsoncallback') + "(" + respon + ")" self.set_header("Access-Control-Allow-Origin", "*") self.write(response)
def update(self,pk,**kwargs): params = [] for k, v in self.__mappings__.iteritems(): #做一次属性校验,将不可更改的剔除 if v.canset == False: if v.name in kwargs: del kwargs[v.name] for k, v in kwargs.iteritems(): if isinstance(v,unicode): ##判断是否已经编码,如果没有就utf-8编码 v = v.encode('utf-8') if isinstance(v,str) == False:## value的值是否是 ‘字符串’ 如果不是,变成字符串 v = str(v) params.append(k + '=' + "'" + v + "'")##;paramers是一个列表 sql = 'update %s set %s where %s=%s' % (self.__table__, ','.join(params),self.__primary_key__,pk) print('SQL: %s' % sql) db = MySQLApp().getInstance() result = db.update(sql) db.close() print(result) return result
def initDB(self, tablename, cls): db = MySQLApp().getInstance() sql = 'select * from ' + tablename db.query(sql) result = db.fetchAllRows() #对行进行循环 for row in result: md = cls(**row) self._m_cache[md.id] = md self._m_cacheKeys.append(md.id) print(md) db.close()