本文主要內(nèi)容python MySQLdb數(shù)據(jù)庫(kù)批量插入insert,更新update的:
元江縣ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!1.python MySQLdb的使用,寫了一個(gè)基類讓其他的sqldb繼承這樣比較方便,數(shù)據(jù)庫(kù)的ip, port等信息使用json配置文件
2.常見的查找,批量插入更新
下面貼出基類代碼:
# _*_ coding:utf-8 _*_ import MySQLdb import json import codecs # 這個(gè)自己改一下啊 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()