本文主要内容python MySQLdb数据库批量插入insert,更新update的:

1.python MySQLdb的使用,写了一个基类让其他的sqldb继承这样比较方便,数据库的ip, port等信息使用json配置文件
2.常见的查找,批量插入更新
下面贴出基类代码:
# _*_ coding:utf-8 _*_
import MySQLdb
import json
import codecs
# 这个自己改一下啊
from utils.JsonUtil import get_json_from_file
def byteify(input):
"""
the string of json typed unicode to str in python
This function coming from stack overflow
:param input: {u'first_name': u'Guido', u'last_name': u'jack'}
:return: {'first_name': 'Guido', 'last_name': 'jack'}
"""
if isinstance(input, dict):
return {byteify(key): byteify(value)
for key, value in input.iteritems()}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
def get_json_from_file(filename):
with open(filename) as jf:
jsondata = json.load(jf)
return byteify(jsondata)
class DbBase(object):
def __init__(self, **kwargs):
self.db_config_file = kwargs['db_config_file']
self.config_db(self.db_config_file)
def config_db(self, db_config_file):
data = get_json_from_file(db_config_file)
host = data['host']
user = data['user']
pwd = data['pwd']
db = data['db']
port = data['port']
self.tb_audit_mobile = data['tb_audit_mobile']
self.conn = MySQLdb.connect(host=host, port=port, user=user, passwd=pwd, db=db, charset="utf8", use_unicode=True)
self.cursor = self.conn.cursor()