Python批量插入SQL Server数据库
因为要做性能测试,需要大量造数据到数据库中,于是用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数据库的更多相关文章
- 将DataTable 数据插入 SQL SERVER 数据库
原文:将DataTable 数据插入 SQL SERVER 数据库 以下提供3中方式将DataTable中的数据插入到SQL SERVER 数据库: 一:使用sqlcommand.executenon ...
- SqlBulkCopy高效能批量插入SQL SERVER
what SqlBulkCopy是.NET提供的用来批量插入数据的一个类,特别是将内存中的数据一次性插入到数据库,目前只能插入到SQL SERVER数据库,数据源可以是DataTable.IDataR ...
- Python 使用Microsoft SQL Server数据库
软件环境: Windows 7 32bit Python 3.6 Download https://www.python.org/downloads/ 默认安装,并添加环境变量,一路Next ... ...
- 用python批量插入数据到数据库中
既然使用python操作数据库必不可少的得使用pymysql模块 可使用两种方式进行下载安装: 1.使用pip方式下载安装 pip install pymysql 2.IDE方式 安装完成后就可以正常 ...
- Python 学习笔记:Python 操作 SQL Server 数据库
最近要将数据写到数据库里,学习了一下如何用 Python 来操作 SQL Server 数据库. 一.连接数据库: 首先,我们要连接 SQL Server 数据库,需要安装 pymssql 这个第三方 ...
- python连接sql server数据库实现增删改查
简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...
- Python监控SQL Server数据库服务器磁盘使用情况
本篇博客总结一下Python采集SQL Server数据库服务器的磁盘使用信息,其实这里也是根据需求不断推进演化的一个历程,我们监控服务器的磁盘走了大概这样一个历程: 1:使用SQL Server作业 ...
- Python 学习 第17篇:从SQL Server数据库读写数据
在Python语言中,从SQL Server数据库读写数据,通常情况下,都是使用sqlalchemy 包和 pymssql 包的组合,这是因为大多数数据处理程序都需要用到DataFrame对象,它内置 ...
- python 使用pymssql连接sql server数据库
python 使用pymssql连接sql server数据库 #coding=utf-8 #!/usr/bin/env python#------------------------------ ...
随机推荐
- 已跳过 'cache' -- 节点处于冲突状态
svn resolved ./cache ./cache 为冲突文件路径“cache”的冲突状态已解决
- 黄聪:Discuz X2.5、3.0、3.1、3.2 如何不用插件实现用户名只允许中文注册
1.在后台--注册与访问--注册链接文字,把“注册”改为“中文注册”或“注册(请使用中文注册)”等 2.后台UCenter管理中心---注册设置---禁止的用户名: *q* *w* *e* * ...
- HttpUrlConnection java.net.SocketException: Software caused connection abort: recv failed
最近做java swing程序在模拟httprequest请求的时候出现了这个错误 java.net.SocketException: Software caused connection abort ...
- java 对象传递 是 值传递 还是 引用传递?
这个问题说实话我感觉没有太大的意义. 按第一印象和c++的一些思想去理解的话对象传递是引用传递,因为传递过去的对象的值能被改变. 但是又有很多人,不知道从哪里扣出来一句,java中只有值传递,没有引用 ...
- PLSQL_闪回操作4_Flashback Drop
2014-06-25 Created By BaoXinjian
- DBA_Oracle Erp版本升级12.1.1到R12.1.3(案例)
20150506 Created By BaoXinjian
- NeHe OpenGL教程 第三十三课:TGA文件
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- suibi 117
-Djava.net.preferIPv4Stack=true re.findall(r"^(\d+)" , content) content = content.replace( ...
- python(22)总结下最近遇到的编码问题
最近爬取,或者解析网页是总是遇到编码问题(我的版本:python2.7) 一.常见异常:UnicodeEncodeError: 'ascii' codec can't encode character ...
- git 版本库回滚(转载)
From:http://www.cnblogs.com/qualitysong/archive/2012/11/27/2791486.html From: http://www.tech126.com ...