• orm功能封装


    封装功能:

    查 : select

    **kwargs.keys() --返回-> obj -转为-->list
    [{},{}] ---> [obj,obj]
    class Models(dict,metaclass=OSError):
        def __getattr__(self, item):
            #调用没有属性时触发
            return self.get(item)
        def __setattr__(self, key, value):
            self[key] = value
            
        @classmethod
        def orm_select(cls,**kwargs):
            mysql = MySQLClient()
            
            if not kwargs:
                sql = 'select * from %s' %cls.table_name
                
                res = mysql.my_select(sql)
                
            else: 
                key = list(kwargs.keys())[0]
                value = kwargs.get(key)
                
                sql = 'select * from %s where %s=? '%(
                    cls.table_name,key
                )
                sql = sql.replace('?','%s')
                res = mysql.my_select(sql,value)
                
            return [cls(**d) for d in res]
    

    增:

        def orm_insert(self):
            mysql = SQLClient()
    
            keys = []
            values = []
            args = []
    
            for k , v in self.mappings.items():
                if not v.primary_key:
    
                    keys.append(v.name)
    
                    values.append(
                        getattr(self,v.name,v.default)
                    )
    
                    args.append('?')
            sql = 'insert into %s(%s) values(%s)' %(
                self.table_name,
                ','.join(keys),
                ','.join(args)
            )
    
            sql = sql.replace('?','%s')
            mysql.my_execute(sql,values)
    
    

    改:

      #更新
      class Models(dict,metaclass=OSError):
        def orm_update(self):
            mysql = SQLClient()
    
            keys = []
            values = []
            primary_key = None
    
            for k,v in self.mappings.items():
                if v.primary_key:
                    primary_key = v.name + '= %s' % getattr(self,v.name)
    
                else:
                    keys.append(v.name + '=?')
                    values.append(
                        getattr(self,v.name)
                    )
    
            sql = 'update %s set %s where %s' %(
                self.table_name,
                ','.join(keys),
                primary_key
            )
            sql = sql.replace('?','%s')
            mysql.my_execute(sql,values)
    
    

    连接数据库:

    连接数据库
    import pymysql
    
    class MySQLClient:
        def __init__(self):
            
            self.client = pymysql.connect(
                host ='localhost',
                port = 3306,
                user= 'root',
                password = '123',
                database = 'orm_demo',
                charset = 'utf-8',
                auto_increment = True
                
            )
            self.cursor = self.client.cursor(
                pymysql.cursors.DictCursor
            )
            
            def my_select(self,sql,value=None):
                self.cursor.execute(sql,value)
                
                res = self.cursor.fetchall()
                
                return res
    
            def my_execute(self, sql, values):
                try:
                    self.cursor.execute(sql, values)
    
                except Exception as e:
                    print(e)
    
            def close(self):
                self.cursor.close()
                self.client.close()
    
    

    操作系统 : C

    手机应用: iphone swift / objective-c

    ​ android java

    ​ 3D : C / C ++

    get / post :

    1. 仅请求资源

      1. 附带用户数据
  • 相关阅读:
    USBkiller 破解绿色版 by TK
    北京理工大学信息安全与对抗竞赛crackme02分析
    EnMp3Player 破解
    IDA相关下载
    猜数游戏 by TK QQ:86935367
    mp3播放器 by TK QQ:86935367
    UPX Easy GUI 汉化版by TK
    superdic cracked by TK
    vc 获取机器码
    (转)UITableView使用
  • 原文地址:https://www.cnblogs.com/shaozheng/p/11803351.html
Copyright © 2020-2023  润新知