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

python實現(xiàn)數(shù)據(jù)庫跨服務(wù)器遷移

 更新時間:2018年04月12日 17:03:35   作者:Stupid_Sparrow  
這篇文章主要為大家詳細(xì)介紹了Python實現(xiàn)數(shù)據(jù)庫之間的數(shù)據(jù)遷移,具有一定的參考價值,感興趣的小伙伴們可以參考一下

 基于Python2.7的版本環(huán)境,Python實現(xiàn)的數(shù)據(jù)庫跨服務(wù)器(跨庫)遷移, 每以5000條一查詢一提交,代碼中可以自行更改每次查詢提交數(shù)目.

# -*- coding: utf-8 -*-

import MySQLdb
import time
import warnings

warnings.filterwarnings("ignore")


class ConnectMysql(object):
  def __init__(self):
#     這里設(shè)置分頁查詢, 每頁查詢多少數(shù)據(jù)
    self.page_size = 5000

  def getTable(self):
    conn = MySQLdb.connect(
      host="***.***.**.**",
      user="****",
      passwd="*************",
      db='****',
      charset='utf8'
    )
    conn_local = MySQLdb.connect(
      host="********************************",
      user="**********",
      passwd="********",
      db='*******',
      charset='utf8'
    )
    cur = conn.cursor()
    cur_local = conn_local.cursor()
    cur.execute('show tables')
    tables = cur.fetchall()
    for table in tables:
      print str(table[0]).lower()
      # 需要遷移的數(shù)據(jù)庫查詢表的列數(shù)
      cur.execute("SELECT COUNT(*) FROM information_schema.COLUMNS WHERE table_schema='china' AND table_name='" + table[0] + "'")
      table_col_count = cur.fetchone()
      # print table_col_count[0]
      # 需要遷移的數(shù)據(jù)庫查詢表的結(jié)構(gòu)
      cur.execute('show create table ' + table[0])
      result = cur.fetchall()
      create_sql = result[0][1]
      # 查詢需要遷移的數(shù)據(jù)庫表的數(shù)據(jù)條數(shù)
      cur.execute('select count(*) from ' + table[0])
      total = cur.fetchone()
      page = total[0] / self.page_size
      page1 = total[0] % self.page_size
      if page1 != 0:
        page = page + 1

      # 阿里云數(shù)據(jù)庫創(chuàng)建表
      cur_local.execute("SELECT table_name FROM information_schema.`TABLES` WHERE table_schema='user' AND table_name='" + str(table[0]).lower() + "'")
      table_name = cur_local.fetchone()
      if table_name is None:
        cur_local.execute(create_sql)
      for p in range(0, page):
        while True:
          try:
            print '開始', table[0], '的第', p + 1, '頁查詢'
            if p == 0:
              limit_param = ' limit ' + str(p * self.page_size) + ',' + str(self.page_size)
            else:
              limit_param = ' limit ' + str(p * self.page_size + 1) + ',' + str(self.page_size)
            cur.execute('select * from ' + table[0] + limit_param)
            inserts = cur.fetchall()
            print '查詢成功'
            param = ''
            for i in range(0, table_col_count[0]):
              param = param + '%s,'
            print '開始插入'
            cur_local.executemany('replace into ' + table[0] + ' values (' + param[0:-1] + ')', inserts)
            print table[0], '的第', p + 1, '頁, 插入完成, 還有', page - p - 1, '頁, 任重而道遠(yuǎn)'
            conn_local.commit()
            break
          except Exception as e:
            print e
            time.sleep(60)
            cur = conn.cursor()
            cur_local = conn_local.cursor()
        print table[0], ' 插入完成'
        print '\n \n ======================================================================== \n\n'
    cur_local.close()
    conn_local.close()
    cur.close()
    conn.close()


if __name__ == '__main__':
  conn_mysql = ConnectMysql()
  conn_mysql.getTable()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用Python進(jìn)行Excel文件xls/xlsx/xsv格式互相轉(zhuǎn)換

    使用Python進(jìn)行Excel文件xls/xlsx/xsv格式互相轉(zhuǎn)換

    本文介紹了如何使用Python進(jìn)行Excel文件格式的互相轉(zhuǎn)換,包括xls到xlsx、xlsx到xls、xls到csv、xlsx到csv、csv到xls以及csv到xlsx的轉(zhuǎn)換方法,轉(zhuǎn)換過程中需要注意文件路徑的修改以及文件名沖突的處理,需要的朋友可以參考下
    2025-11-11
  • PyQt5 加載圖片和文本文件的實例

    PyQt5 加載圖片和文本文件的實例

    今天小編就為大家分享一篇PyQt5 加載圖片和文本文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • 在MAC上搭建python數(shù)據(jù)分析開發(fā)環(huán)境

    在MAC上搭建python數(shù)據(jù)分析開發(fā)環(huán)境

    這篇文章主要介紹了在MAC上搭建python數(shù)據(jù)分析開發(fā)環(huán)境的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Tensorflow 訓(xùn)練自己的數(shù)據(jù)集將數(shù)據(jù)直接導(dǎo)入到內(nèi)存

    Tensorflow 訓(xùn)練自己的數(shù)據(jù)集將數(shù)據(jù)直接導(dǎo)入到內(nèi)存

    這篇文章主要介紹了Tensorflow 訓(xùn)練自己的數(shù)據(jù)集將數(shù)據(jù)直接導(dǎo)入到內(nèi)存,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • 對python3中的RE(正則表達(dá)式)-詳細(xì)總結(jié)

    對python3中的RE(正則表達(dá)式)-詳細(xì)總結(jié)

    今天小編就為大家分享一篇對python3中的RE(正則表達(dá)式)-詳細(xì)總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • 最新評論

    堆龙德庆县| 宜宾县| 韶关市| 盐山县| 肇东市| 黄浦区| 抚松县| 扎囊县| 鸡西市| 六盘水市| 商丘市| 博客| 石首市| 阿勒泰市| 孝昌县| 安塞县| 稻城县| 崇州市| 盐池县| 锡林浩特市| 陇川县| 连平县| 滨州市| 北辰区| 兴海县| 兴业县| 信丰县| 西丰县| 深水埗区| 普安县| 内乡县| 牙克石市| 陵水| 昭通市| 昆山市| 夏河县| 轮台县| 长沙县| 车险| 襄樊市| 宜君县|