日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)Python教程:python如何轉(zhuǎn)移數(shù)據(jù)庫(kù)里的數(shù)據(jù)

1、常見(jiàn)數(shù)據(jù)庫(kù)

目前創(chuàng)新互聯(lián)已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、榆社網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

(1)Scikit-learn:需要復(fù)蓋特征工程、模型訓(xùn)練和模型測(cè)試所有功能的程序庫(kù),Scikit-learn是最好的選擇。這個(gè)優(yōu)秀的免費(fèi)軟件可以提供機(jī)器學(xué)習(xí)和數(shù)據(jù)挖掘所需的所有工具,現(xiàn)在是python機(jī)器學(xué)習(xí)的標(biāo)準(zhǔn)庫(kù),建議使用成熟的機(jī)器學(xué)習(xí)算法。

(2)NLTK:雖然不是機(jī)器學(xué)習(xí)的程序庫(kù),但它是自然語(yǔ)言處理所必需的庫(kù)。除了文本處理功能之外,它還包括聚類(lèi)、分詞、詞干提取、標(biāo)記、分析等大量數(shù)據(jù)集和其他關(guān)于詞法的資源。

2、轉(zhuǎn)移數(shù)據(jù)

基于Python2.7的版本環(huán)境,Python實(shí)現(xiàn)了數(shù)據(jù)庫(kù)的跨服務(wù)器遷移,每次提交查詢都要5000條,代碼中每個(gè)查詢提交的數(shù)量可以自己改變。

# -*- coding: utf-8 -*-
 
import MySQLdb
import time
import warnings
 
warnings.filterwarnings("ignore")
 
 
class ConnectMysql(object):
    def __init__(self):
#         這里設(shè)置分頁(yè)查詢, 每頁(yè)查詢多少數(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ù)庫(kù)查詢表的列數(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ù)庫(kù)查詢表的結(jié)構(gòu)
            cur.execute('show create table ' + table[0])
            result = cur.fetchall()
            create_sql = result[0][1]
            # 查詢需要遷移的數(shù)據(jù)庫(kù)表的數(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ù)庫(kù)創(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 '開(kāi)始', table[0], '的第', p + 1, '頁(yè)查詢'
                        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 '開(kāi)始插入'
                        cur_local.executemany('replace into ' + table[0] + ' values (' + param[0:-1] + ')', inserts)
                        print table[0], '的第', p + 1, '頁(yè), 插入完成, 還有', page - p - 1, '頁(yè), 任重而道遠(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()

以上就是python轉(zhuǎn)移數(shù)據(jù)庫(kù)里數(shù)據(jù)的方法,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程

本文教程操作環(huán)境:windows7系統(tǒng)、Python 2.7,DELL G3電腦。


分享題目:創(chuàng)新互聯(lián)Python教程:python如何轉(zhuǎn)移數(shù)據(jù)庫(kù)里的數(shù)據(jù)
新聞來(lái)源:http://www.5511xx.com/article/dpdogsj.html