Python-批量插入


#!/usr/bin/python
# -*- coding: UTF-8 -*-

import MySQLdb
import traceback
import time

# 測試環境腳本 - 批量從Mysql主表導出數據到Mycat子表

# Mysql: 打開數據庫連接
mysqlDB = MySQLdb.connect(host="47.91", port=3306, user="s", passwd="s", db="s", charset='utf8' )
mysqlCursor = mysqlDB.cursor()
# Mycat: 打開數據庫連接
mycatDB = MySQLdb.connect(host="47.89", port=8066, user="s", passwd="s", db="s", charset='utf8' )
mycatCursor = mycatDB.cursor()

# 截止時間
endTime = "2018-06-20 15:20:00"
# 頁大小
pageSize = 500

try:
    # 循環分頁獲取時間線數據
    page = 1
    while (page > 0):

        # 分頁查詢
        offset = (page - 1) * pageSize
        sql = "SELECT * FROM t_contacts_timeline WHERE main_user_id IS NOT NULL AND create_time < '%s' ORDER BY create_time ASC LIMIT %s,%s " % (endTime, offset, pageSize)
        fetchCount = mysqlCursor.execute(sql)

        # 如果未查到數據, 中斷執行
        if fetchCount == 0:
            break
        mysqlResults = mysqlCursor.fetchall()

        # 拼裝導入SQL域名
        inserIntoSql = 'INSERT ignore INTO t_contacts_timeline ' \
                       '(id, user_id, contact_id, contact_type, create_time, social_type, social_content, refer_id, top_flag, update_time, read_flag, delete_tag, home_page, browse_end_time, msg_time, page_action_logs, parent_id, main_user_id) VALUES ' \
                       '(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
        valuesList = []
        for row in mysqlResults:
            valueTup = (row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[11],row[12],row[13],row[14],row[15],row[16],row[17])
            valuesList.append(valueTup)

        # 批量導入,發生異常,中斷執行
        try:
            mycatCursor.executemany(inserIntoSql, valuesList)
            mycatDB.commit()
        except Exception, e:
            mycatDB.rollback()
            print u"[批量導入-中斷] valuesList=%s" % (valuesList[0])
            print u"[批量導入-異常] msg=%s" % traceback.print_exc()
            break
        page = page + 1

except Exception,e:
    print u"[執行發生異常] msg=%s" % traceback.print_exc()

# 關閉數據庫連接
mysqlCursor.close()
mysqlDB.close()
mycatCursor.close()
mycatDB.close()
print u"執行完成!"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM