因为要做性能测试,需要大量造数据到数据库中,于是用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. Nginx做反向代理总是被系统kill

    公司使用Nginx做反向代理,以前都挺正常的,最近不知怎么回事总是无端被系统kill,而在nginx错误日志中也没有信息输出. 网上查了很多资料,也没什么靠谱的回答,唯一觉得有点关联的就是linux ...

  2. MIT牛人解说数学体系(转载)

    原文网址:http://www.guokr.com/post/442622/ 在过去的一年中,我一直在数学的海洋中游荡,research进展不多,对于数学世界的阅历算是有了一些长进. 为什么要深入数学 ...

  3. js常用方法收集

    JS获取地址栏制定参数值: //获取URL参数的值 function getUrlParam(name){ var reg = new RegExp("(^|&)"+ na ...

  4. JavaScript权威指南 第二章 词法结构

    这章主要把保留字说一下 JavaScript 把一些标识符拿出来用做自己的关键字.因此,就不能再在程序中把这些关键字用做标识符了: break delete function return typeo ...

  5. 黄聪:simple_html_dom 换行符丢失

    我在利用simple_html_dom来解析文档是,想要将其中的换行符替换成<BR> , 结果试了好几次没有成功,但是在原始文档中确实是有换行符的.后来索性把装载进来的文档打印出来,结果发 ...

  6. (C++) Interview in English. - Constructors/Destructors

    Constructors/Destructors. 我们都知道,在C++中建立一个类,这个类中肯定会包括构造函数.析构函数.复制构造函数和重载赋值操作:即使在你没有明确定义的情况下,编译器也会给你生成 ...

  7. sql server中的左连接与右连接的简便写法

    左连接 *=(左表中的数据全部显示出来,右表中没有相关联的数据显示null) select Users.*,Department.name as DepartmentName from Users,D ...

  8. [Android Exception A] – 1-The following classes could not be instantiated

    http://stackoverflow.com/questions/26575815/the-following-classes-could-not-be-instantiated-android- ...

  9. 使用SparkSQL实现多线程分页查询并写入文件

    一.由于具有多张宽表且字段较多,每个宽表数据大概为4000万条,根据业务逻辑拼接别名,并每张宽表的固定字段进行left join 拼接SQL.这样就能根据每个宽表的主列,根据每个宽表的不同字段关联出一 ...

  10. hdu 5363 组合数学 快速幂

    Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 131072/131072 K (Java/Others) Problem Descrip ...