因为要做性能测试,需要大量造数据到数据库中,于是用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()

Python批量插入SQL Server数据库的更多相关文章

  1. 将DataTable 数据插入 SQL SERVER 数据库

    原文:将DataTable 数据插入 SQL SERVER 数据库 以下提供3中方式将DataTable中的数据插入到SQL SERVER 数据库: 一:使用sqlcommand.executenon ...

  2. SqlBulkCopy高效能批量插入SQL SERVER

    what SqlBulkCopy是.NET提供的用来批量插入数据的一个类,特别是将内存中的数据一次性插入到数据库,目前只能插入到SQL SERVER数据库,数据源可以是DataTable.IDataR ...

  3. Python 使用Microsoft SQL Server数据库

    软件环境: Windows 7 32bit Python 3.6  Download https://www.python.org/downloads/ 默认安装,并添加环境变量,一路Next ... ...

  4. 用python批量插入数据到数据库中

    既然使用python操作数据库必不可少的得使用pymysql模块 可使用两种方式进行下载安装: 1.使用pip方式下载安装 pip install pymysql 2.IDE方式 安装完成后就可以正常 ...

  5. Python 学习笔记:Python 操作 SQL Server 数据库

    最近要将数据写到数据库里,学习了一下如何用 Python 来操作 SQL Server 数据库. 一.连接数据库: 首先,我们要连接 SQL Server 数据库,需要安装 pymssql 这个第三方 ...

  6. python连接sql server数据库实现增删改查

    简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...

  7. Python监控SQL Server数据库服务器磁盘使用情况

    本篇博客总结一下Python采集SQL Server数据库服务器的磁盘使用信息,其实这里也是根据需求不断推进演化的一个历程,我们监控服务器的磁盘走了大概这样一个历程: 1:使用SQL Server作业 ...

  8. Python 学习 第17篇:从SQL Server数据库读写数据

    在Python语言中,从SQL Server数据库读写数据,通常情况下,都是使用sqlalchemy 包和 pymssql 包的组合,这是因为大多数数据处理程序都需要用到DataFrame对象,它内置 ...

  9. python 使用pymssql连接sql server数据库

    python 使用pymssql连接sql server数据库   #coding=utf-8 #!/usr/bin/env python#------------------------------ ...

随机推荐

  1. Python Beautiful Soup模块的安装

    以安装Beautifulsoup4为例: 1.到网站上下载:http://www.crummy.com/software/BeautifulSoup/bs4/download/ 2.解压文件到C:\P ...

  2. C基础--函数参数副本

    转自:http://blog.csdn.net/chujiangke001/article/details/38553173 void GetMemory(char *p, int num) { p ...

  3. 深度优化LNMP

    优化前准备工作 Centos准备及配置 准备安装包及软件:http://pan.baidu.com/s/1chHQF  下载解压到U盘即可安装http://pan.baidu.com/s/15TUWf ...

  4. 黄聪:PHP json_encode中文乱码解决方法

    相信很多人在使用Ajax与后台php页面进行交互的时候都碰到过中文乱码的问题.JSON作为一种轻量级的数据交换格式,备受亲睐,但是用PHP作为后台交互,容易出现中文乱码的问题.JSON和js一样,对于 ...

  5. C#数字千分位问题

    1.C#中用最简单的方法把数字(不含小数)转换为千分位格式:     如1234567变成1,234,567 方法:1234567.ToString("###,###")   或  ...

  6. 开发高峰时的CPU使用率

  7. 20160720-java高并发

    https://www.zhihu.com/search?type=content&q=tomcat+%E8%83%BD%E6%94%AF%E6%8C%81%E5%A4%9A%E5%B0%91 ...

  8. Edmonds_Karp 算法入门详解(转)

    转载自:http://blog.csdn.net/hsqlsd/article/details/7862903 有n个点,有m条有向边,有一个点很特殊,只出不进,叫做源点,通常规定为1号点.另一个点也 ...

  9. Python 向上取整的算法

    一.初衷: 有时候我们分页展示数据的时候,需要计算页数.一般都是向上取整,例如counts=205 pageCouts=20 ,pages= 11 页. 一般的除法只是取整数部分,达不到要求. 二.方 ...

  10. Python 结巴分词(2)关键字提取

    提取关键字的文章是,小说完美世界的前十章: 我事先把前十章合并到了一个文件中: 然后直接调用关键字函数: import sys sys.path.append('../') import jieba ...