zoukankan      html  css  js  c++  java
  • 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'删除数据库成功'
  • 相关阅读:
    1.LOAM安装
    查看memcached运行状态
    (转)服务器故障排查,侵删
    百度地图tilesloaded只触发一次
    Linux 安装NVIDIA显卡
    Oracle常用sql(持续更新)
    jetBrain idea 常用插件整理
    关于swiper4 一个页面多个轮播的问题
    linux 日常工作常用软件(持续更新)
    WPS for Linux ,Linux平台最好的文档编辑软件,没有之一
  • 原文地址:https://www.cnblogs.com/dvbbs2012/p/7233989.html
Copyright © 2011-2022 走看看