Python批量插入SQL Server数据库

时间:2021-04-17 19:52:54

因为要做性能测试,需要大量造数据到数据库中,于是用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("") + ","\
+ str("") + ","\
+ "'" + "sryan_" + str(cur) + "'" + ","\
+ "'" + "" + str(cst_id) + "'" + ","\
+ "'" + " " + "'" + ","\
+ str("") + ","\
+ "SYSDATETIME()" + ","\
+ "'" + "sryan" + "'" + ","\
+ "SYSDATETIME()" + ","\
+ "'" + "sryan" + "'" + ","\
+ str("")\
+ ")"
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("") + ","\
+ str("") + ","\
+ str("") + ","\
+ "'" + "" + str(random.randint(1111, 9999)) + "'"\
+ ")"
return instr @staticmethod
def bcustomer_reward(cst_id):
instr = "insert into BCUSTOMERREWARD values ("\
+ "'" + "" + str(cst_id) + "'" + ","\
+ str("") + ","\
+ str("") + ","\
+ "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) + "'" + ","\
+ "'" + "" + 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()