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#------------------------------ ...
随机推荐
- Php检测文件编码方法
<?php /** * 检测文件编码 * @param string $file 文件路径 * @return string|null 返回 编码名 或 null */ function det ...
- eclipse常用插件安装
打开资源文件所在目录 EasyExplorer 下载:http://sourceforge.net/projects/easystruts/ 直接拷贝到%ECLIPSE_HOME%\plugins 或 ...
- Titan-红号楼宗谱案例
一. 简介 titan:存储,查询图形结构的数据库.分布式集群环境下,可支持数以千亿级别的点和边,同时支持上千个并发的实时的复杂图形遍历,支持ACID事务. 架构:支持以下3方面的自由组合 (1)节点 ...
- hibernateUtils
hibernate3.x版本 package hibernate_01; import org.hibernate.Session; import org.hibernate.SessionFacto ...
- Oracle Erp常用网站
2014-01-01 Created By BaoXinjian
- 如何判断TCP包是否发送成功
1. TCP发送接口:send() TCP发送数据的接口有send,write,sendmsg.在系统内核中这些函数有一个统一的入口,即sock_sendmsg().由于TCP是可靠传输,所以对TCP ...
- 问对于一个给定的n,怎样才能用最少的步骤将它变到1
如果n为偶数,则将它除以2,如果n为奇数,则将它加1或者减1.问对于一个给定的n,怎样才能用最少的步骤将它变到1.例如:n= 61n-- 60n/2 30n/2 15n++ 16n/2 8n/2 4n ...
- HTTP 错误 500.19 - Internal Server Error
ylbtech-Error-IIS: HTTP 错误 500.19 - Internal Server Error 1.A,错误代码返回顶部 错误摘要 HTTP 错误 500.19 - Interna ...
- Ecshop(二次开发) - 后台添加左侧菜单导航
1.链接地址:修改 admin\includes\inc_menu.php 文件. $modules['17_syn_data']['view_syn'] = 'synchroni ...
- Windows下编译使用Aliyun OSS PHP SDK
摘要: WIN环境下搭建Aliyun OSS PHP SDK编译运行环境.从PHP的安装逐步完成,SDK的编译运行.即使没有任何PHP基础,也能顺利完成. 安装环境:Win7 64 + PHP 5.6 ...