Esempio n. 1
0
 def save(self):
     ms = mysql_pool.MysqlPool()
     fields = []
     params = []
     args = []
     for k, v in self.mapping.items():
         fields.append(v.name)
         params.append('?')
         args.append(getattr(self, k, v.default))
     sql = "insert into %s (%s) values (%s)" % (self.table_name, ','.join(fields), ','.join(params))
     sql = sql.replace('?', '%s')
     ms.execute(sql, args)
Esempio n. 2
0
 def select_all(cls, **kwargs):
     ms = mysql_pool.MysqlPool()
     if kwargs:  # 当有参数传入的时候
         key = list(kwargs.keys())[0]
         value = kwargs[key]
         sql = "select * from %s where %s=?" % (cls.table_name, key)
         sql = sql.replace('?', '%s')
         re = ms.select(sql, value)
     else:  # 当无参传入的时候查询所有
         sql = "select * from %s" % cls.table_name
         re = ms.select(sql)
     return [cls(**r) for r in re]
Esempio n. 3
0
    def select_one(cls, **kwargs):
        # 此处只支持单一条件查询
        key = list(kwargs.keys())[0]
        value = kwargs[key]
        ms = mysql_pool.MysqlPool()
        sql = "select * from %s where %s=?" % (cls.table_name, key)

        sql = sql.replace('?', '%s')
        re = ms.select(sql, value)
        if re:
            return cls(**re[0])
        else:
            return None
Esempio n. 4
0
    def update(self):
        ms = mysql_pool.MysqlPool()
        fields = []
        args = []
        pr = None
        for k, v in self.mapping.items():
            if v.primary_key:
                pr = getattr(self, k, None)
            else:
                fields.append(v.name + '=?')
                args.append(getattr(self, k, v.default))
        sql = "update %s set %s where %s = %s" % (
            self.table_name, ', '.join(fields), self.primary_key, pr)

        sql = sql.replace('?', '%s')
        print(sql)
        ms.execute(sql, args)