最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python MySQLdb使用教程詳解

 更新時間:2018年03月20日 10:08:53   作者:FishBear_move_on  
本文主要內(nèi)容python MySQLdb數(shù)據(jù)庫批量插入insert,更新update的相關(guān)知識,感興趣的朋友跟隨腳本之家小編一起看看吧

本文主要內(nèi)容python MySQLdb數(shù)據(jù)庫批量插入insert,更新update的:

1.python MySQLdb的使用,寫了一個基類讓其他的sqldb繼承這樣比較方便,數(shù)據(jù)庫的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()

子類的示例:

class DbAuditTestService(DbBase):
  def __init__(self, **kwargs):
    super(DbAuditTestService, self).__init__(**kwargs)
  def getAdTestURl(self, beg, end):
    sql = """select url, source from tb_name where create_date BETWEEN '%s' and '%s' """ % (beg, end)
    self.cursor.execute(sql)
    res = [row for row in self.cursor]
    return res
  def insert(self, lst_row):
  """batch insert, use ignore 避免索引唯一問題"""
    try:
      insert_sql = 'INSERT ignore INTO tb_ms_mobile_report_test (appid, source) VALUES (%s, %s)'
      self.cursor.executemany(insert_sql, lst_row)
      self.conn.commit()
    except MySQLdb.OperationalError as e:
      logger.info('%s' % e)
      self.cursor.close()
      self.conn.close()
      self.config_db(self.db_config_file)
  def update_ip_info(self, ip_info):
    """
    batch update 
      [[voilate_right_rate, ip]]
    :param ip_info:
    :return:
    """
    query = """
        update tb_ms_audit_ip_info set
        ip_right_rate=%s
        where submit_ip=%s """
    self.cursor.executemany(query, ip_info)
    self.conn.commit()
def insert_all():
"""批量操作的示例"""
  db_audit = DbAuditService(db_config_file='../config/mysql_police_audit.json')
  size = db_audit.count()
  db_audit_test = DbAuditTestService(db_config_file='../config/mysql_local_audit.json')
  batch_size = 2000
  for k in xrange(100000, size, batch_size):
    logger.info('query limit %s ~ %s' % (k, batch_size))
    lst_row = db_audit.query_limit(k, batch_size)
    logger.info('convert_rows ')
    lst_row = convert_rows(lst_row)
    db_audit_test.insert(lst_row)

總結(jié)

以上所述是小編給大家介紹的python MySQLdb使用教程詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Python根據(jù)字典的值查詢出對應(yīng)的鍵的方法

    Python根據(jù)字典的值查詢出對應(yīng)的鍵的方法

    這篇文章主要介紹了Python根據(jù)字典的值查詢出對應(yīng)的鍵的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • python發(fā)送郵件腳本

    python發(fā)送郵件腳本

    這篇文章主要為大家詳細介紹了發(fā)送郵件python腳本,支持多個附件,中文,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Python 輸入一個數(shù)字判斷成績分數(shù)等級的方法

    Python 輸入一個數(shù)字判斷成績分數(shù)等級的方法

    今天小編就為大家分享一篇Python 輸入一個數(shù)字判斷成績分數(shù)等級的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python的type函數(shù)結(jié)果你知道嘛

    Python的type函數(shù)結(jié)果你知道嘛

    這篇文章主要為大家介紹了Python的type函數(shù)結(jié)果,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • 使用jupyter notebook運行python和R的步驟

    使用jupyter notebook運行python和R的步驟

    這篇文章主要介紹了使用jupyter notebook運行python和R的步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • python對raw格式照片進行降噪處理的方法詳解

    python對raw格式照片進行降噪處理的方法詳解

    要對RAW格式的照片進行降噪,我們可以使用rawpy庫來讀取RAW圖像,并使用imageio庫將處理后的圖像保存為其他格式,如PNG或JPEG,本文將詳細給大家介紹python如何對raw格式照片進行降噪處理,文中有詳細的代碼流程,需要的朋友可以參考下
    2023-05-05
  • Python性能分析工具Profile使用實例

    Python性能分析工具Profile使用實例

    這篇文章主要介紹了Python性能分析工具Profile使用實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • python皮爾遜相關(guān)性數(shù)據(jù)分析分析及實例代碼

    python皮爾遜相關(guān)性數(shù)據(jù)分析分析及實例代碼

    這篇文章主要為大家介紹了python皮爾遜相關(guān)性分析及實例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Python?中10進制數(shù)與16進制數(shù)相互轉(zhuǎn)換問題

    Python?中10進制數(shù)與16進制數(shù)相互轉(zhuǎn)換問題

    這篇文章主要介紹了Python中10進制數(shù)與16進制數(shù)相互轉(zhuǎn)換,在Python中,我們可以使用內(nèi)置的hex()函數(shù)將10進制數(shù)轉(zhuǎn)換為16進制數(shù),需要的朋友可以參考下
    2023-05-05
  • python實現(xiàn)批量改文件名稱的方法

    python實現(xiàn)批量改文件名稱的方法

    這篇文章主要介紹了python實現(xiàn)批量改文件名稱的方法,涉及Python中os模塊rename方法的相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05

最新評論

枝江市| 三原县| 武宁县| 时尚| 罗源县| 西乌珠穆沁旗| 军事| 得荣县| 巢湖市| 平乐县| 镇平县| 岳池县| 富锦市| 社会| 天长市| 宜兰市| 济南市| 渑池县| 沙田区| 秦安县| 萨嘎县| 扎囊县| 双鸭山市| 孟州市| 乡城县| 旬邑县| 阿拉善左旗| 龙泉市| 洛南县| 衡水市| 郑州市| 密云县| 黄大仙区| 库伦旗| 福州市| 临潭县| 股票| 遵义县| 汝南县| 永川市| 稻城县|