1. 程式人生 > 實用技巧 >Mysql 跨庫資料遷移 -- python 指令碼

Mysql 跨庫資料遷移 -- python 指令碼

示例程式碼

import pymysql

class DataMigrate:

    def __init__(self, env):
        self.num = 50
        self.ENV = env

        # From
        self.db_from = self.connect_jingyi(1)
        self.cursor_from = self.db_from.cursor()
        self.from_sql = 'select first_project, status from product '

        #
To self.db_to = self.connect_jingyi() self.cursor_to = self.db_to.cursor() self.to_sql = "insert into product_copy(first_project, status) value(%s,%s)" def connect_jingyi(self, is_jingyi=0): if is_jingyi: if self.ENV == 'test': return pymysql.connect(host='
****', port=3306, user='***', passwd='***', db='from_db', charset='utf8' )
else: return pymysql.connect(host='****', port=3306, user='***', passwd='***', db='from_db', charset='utf8' ) else: if self.ENV == 'test': return pymysql.connect(host='****', port=3306, user='***', passwd='***', db='to_db', charset='utf8' ) elif self.ENV == 'pre': return pymysql.connect(host='*****', port=3306, user='***', passwd='***', db='to_db', charset='utf8' ) def migrate_date(self): len_from = self.cursor_from.execute(self.from_sql) for i in range(int(len_from / self.num)): print(i) data1 = self.cursor_from.fetchmany(self.num) self.cursor_to.executemany(self.to_sql, data1) data2 = self.cursor_from.fetchall() self.cursor_to.executemany(self.to_sql, data2) self.db_to.commit() self.db_from.close() self.db_to.close() if __name__ == '__main__': data_migrate = DataMigrate('self-test') data_migrate.migrate_date()

函式解析

fetchone() 獲取遊標下一條資料(元組)

fetchmany() 獲取多條資料(元組列表)

fetchall() 獲取所有資料(元組列表)

看下原始碼

...
    def fetchone(self):
        """Fetch the next row"""
        self._check_executed()
        if self._rows is None or self.rownumber >= len(self._rows):
            return None
        result = self._rows[self.rownumber]
        self.rownumber += 1
        return result

    def fetchmany(self, size=None):
        """Fetch several rows"""
        self._check_executed()
        if self._rows is None:
            return ()
        end = self.rownumber + (size or self.arraysize)
        result = self._rows[self.rownumber:end]
        self.rownumber = min(end, len(self._rows))
        return result

    def fetchall(self):
        """Fetch all the rows"""
        self._check_executed()
        if self._rows is None:
            return ()
        if self.rownumber:
            result = self._rows[self.rownumber:]
        else:
            result = self._rows
        self.rownumber = len(self._rows)
        return result
...