• Python操作MySQL


    #!/usr/bin/env python 
    #coding:utf-8
    
    import  MySQLdb
    import  config
    
    class MySQLHelper(object):
        def __init__(self):
            self.conn=config.conn_dict
    
        def get_one(self,sql,params):
            conn = MySQLdb.connect(**self.conn)
            cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
            retCount = cur.execute(sql,params)
            data = cur.fetchone()
            cur.close()
            conn.close()
            return data
    
        def get_many(self,sql,params):
            conn = MySQLdb.connect(**self.conn)
            cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
            retCount = cur.execute(sql,params)
            data = cur.fetchall()
            cur.close()
            conn.close()
            return data
    
        def insert_one(self,sql,params):
            conn = MySQLdb.connect(**self.conn)
            cur = conn.cursor()
            cur.execute(sql, params)
            conn.commit()
            cur.close()
            return u'插入数据库成功'
    
        def insert_many(self,sql,params):
            conn = MySQLdb.connect(**self.conn)
            cur = conn.cursor()
            cur.executemany(sql, params)
            conn.commit()
            cur.close()
            return u'批量插入数据库成功'
    
        def update_one(self,sql,params):
            conn = MySQLdb.connect(**self.conn)
            cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
            ret = cur.execute(sql, params)
            conn.commit()
            cur.close()
            conn.close()
            return u'更新数据库成功'
    
        def delete_one(self,sql,params):
            conn = MySQLdb.connect(**self.conn)
            cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
            ret = cur.execute(sql, params)
            conn.commit()
            cur.close()
            conn.close()
            return u'删除数据库成功'
  • 相关阅读:
    面试题总结(vue面试题)
    面试题总结(css面试题)
    设置div居中显示
    关于js中iframe 中 location.href的用法
    js判断是否在iframe中
    npm git 常用命令行 记录
    mongDB数据库 小白学习
    EJS 入门学习
    bower 基础认识
    gulp 粗粗学习 记录下
  • 原文地址:https://www.cnblogs.com/dvbbs2012/p/7233989.html
Copyright © 2020-2023  润新知