• django-pymysql-封装的sql使用


    封装的sql

    import pymysql
    
    
    def get_list(sql,args):
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='s4db65', charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cursor.execute(sql,args)
        result = cursor.fetchall()
        cursor.close()
        conn.close()
        return result
    
    def get_one(sql,args):
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='s4db65', charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cursor.execute(sql,args)
        result = cursor.fetchone()
        cursor.close()
        conn.close()
        return result
    
    def modify(sql,args):
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='s4db65', charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cursor.execute(sql,args)
        conn.commit()
        cursor.close()
        conn.close()
    
    def create(sql,args):
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='s4db65', charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cursor.execute(sql,args)
        conn.commit()
        last_row_id = cursor.lastrowid
        cursor.close()
        conn.close()
        return last_row_id
    
    
    class SqlHelper(object):
        def __init__(self):
            # 读取配置文件
            self.connect()
    
        def connect(self):
            self.conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='s4db65', charset='utf8')
            self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
    
        def get_list(self,sql,args):
            self.cursor.execute(sql,args)
            result = self.cursor.fetchall()
            return result
    
        def get_one(self,sql,args):
            self.cursor.execute(sql,args)
            result = self.cursor.fetchone()
            return result
    
        def modify(self,sql,args):
            self.cursor.execute(sql,args)
            self.conn.commit()
    
        def multiple_modify(self,sql,args):
            # self.cursor.executemany('insert into bd(id,name)values(%s,%s)',[(1,'alex'),(2,'eric')])
            self.cursor.executemany(sql,args)
            self.conn.commit()
    
        def create(self,sql,args):
            self.cursor.execute(sql,args)
            self.conn.commit()
            return self.cursor.lastrowid
    
        def close(self):
            self.cursor.close()
            self.conn.close()
  • 相关阅读:
    poj 1573 Robot Motion
    poj 1035 Spell checker
    poj 3080 Blue Jeans
    poj 3468 A Simple Problem with Integers(线段树区间更新)
    poj 3687 Labeling Balls(拓补排序)
    poj 3295 Tautology
    poj 1062 昂贵的聘礼
    hdu 1170 Balloon Comes!
    hdu 1215 七夕节
    OCJP-试题集合 | 对象的比较
  • 原文地址:https://www.cnblogs.com/yangxiang1/p/7082201.html
Copyright © 2020-2023  润新知