#!/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'删除数据库成功'