码迷,mamicode.com
首页 > 数据库 > 详细

Python批量插入SQL Server数据库

时间:2015-12-10 16:31:47      阅读:378      评论:0      收藏:0      [点我收藏+]

标签:

因为要做性能测试,需要大量造数据到数据库中,于是用python写了点代码去实现,批量插入,一共四张表

简单粗暴地插入10万条数据

import pymssql
import random

__author__ = sryan


class GenerateData:
    def __init__(self):
        print(init)
        self.conn = None
        self.cur = None

    def connect(self, host, user, password, database):
        try:
            self.conn = pymssql.connect(host=host, user=user, password=password, database=database)
            self.cur = self.conn.cursor()
        except Exception, e:
            print e.message

    def disconnect(self):
        try:
            if self.conn is not None:
                self.cur.close()
                self.conn.close()
        except Exception, e:
            print e.message

    def insert_script(self, str_insert, times=1):
        for i in range(times):
            self.cur.execute(str_insert)

    @staticmethod
    def bcustomer_mzn(cst_id, cur):
        instr = "insert into BCUSTOMER_MZN (CST_ID, CST_CTT_ID, CST_GND_ID,"                  + "CST_NAME, CST_MEMBERSHIPNUM, CST_MOBILE, CST_DELETED_FLAG,CST_CREATION_DT,"                  + "CST_CREATIONUID, CST_UPDATE_DT, CST_UPDATEUID, CST_MNGT_CNT_ID) values ("                  + "" + str(cst_id) + "" + ","                  + str("1") + ","                  + str("1") + ","                  + "" + "sryan_" + str(cur) + "" + ","                  + "" + "100" + str(cst_id) + "" + ","                  + "" + " " + "" + ","                  + str("0") + ","                  + "SYSDATETIME()" + ","                  + "" + "sryan" + "" + ","                  + "SYSDATETIME()" + ","                  + "" + "sryan" + "" + ","                  + str("1")                  + ")"
        return instr

    @staticmethod
    def xcustomer_mzn(cst_id):
        instr = "insert into XCUSTOMER_MZN (XCS_CST_ID,XCS_ADT_ID,XCS_MRT_ID,XCS_RVT_ID,XCS_IDCARDNUM) values ("                + "" + str(cst_id) + "" + ","                + str("1") + ","                + str("2") + ","                + str("1") + ","                + "" + "32060219800618" + str(random.randint(1111, 9999)) + ""                + ")"
        return instr

    @staticmethod
    def bcustomer_reward(cst_id):
        instr = "insert into BCUSTOMERREWARD values ("                + "" + "100" + str(cst_id) + "" + ","                + str("1") + ","                + str("2") + ","                + "null" + ","                + "null" + ","                + "null" + ","                + "null" + ","                + "null"                + ")"
        return instr

    @staticmethod
    def bmembership_mzn(cst_id):
        instr = "insert into BMEMBERSHIP_MZN (MMB_ID,MMB_CST_ID,MMB_MEMBERSHIPNUM,"                + "MMB_ISSUE_DT,MMB_START_DT,MMB_CREATION_DT,MMB_UPDATE_DT) values ("                + "" + str(cst_id) + "" + ","                + "" + str(cst_id) + "" + ","                + "" + "100" + str(cst_id) + "" + ","                + "SYSDATETIME()" + ","                + "SYSDATETIME()" + ","                + "SYSDATETIME()" + ","                + "SYSDATETIME()"                + ")"
        return instr

    @staticmethod
    def xmembership_mzn(cst_id):
        instr = "insert into XMEMBERSHIP_MZN (XMB_MMB_ID,XMB_CREATION_DT,"                + "XMB_CREATIONUID,XMB_UPDATE_DT,XMB_UPDATEUID) values("                + "" + str(cst_id) + "" + ","                + "SYSDATETIME()" + ","                + "" + "sryan" + "" + ","                + "SYSDATETIME()" + ","                + "" + "sryan" + ""                + ")"
        return instr

# 测试代码
d = GenerateData()
d.connect(xx.xxx.xx.xxx, xxx, xxxx, xxxx)
i = 0
for i in range(1, 100000):
    cstid = 2000000 + i
    insert = GenerateData.bcustomer_mzn(cstid, i)
    d.cur.execute(insert)
    insert = GenerateData.xcustomer_mzn(cstid)
    d.cur.execute(insert)
    insert = GenerateData.bcustomer_reward(cstid)
    d.cur.execute(insert)
    insert = GenerateData.bmembership_mzn(cstid)
    d.cur.execute(insert)
    insert = GenerateData.xmembership_mzn(cstid)
    d.cur.execute(insert)

d.cur.execute("select * from XMEMBERSHIP_MZN where XMB_MMB_ID = 2000505")
print d.cur.fetchall()
d.conn.commit()

 

Python批量插入SQL Server数据库

标签:

原文地址:http://www.cnblogs.com/ryansunyu/p/5036240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!