参考link:

https://blog.csdn.net/qq_35958094/article/details/78462800(插入相关)

https://www.cnblogs.com/hyace/p/4173831.html(效率相关)

最近自己参考上面的link写了一个批量插入数据库的小脚本,记录一下,附上代码:

#!/usr/bin/python
# -*- coding:utf-8 -*- import pymysql class InsertData():
def __init__(self, db_name):
# connect to database
self.db = pymysql.connect('127.0.0.1',
'root',
'123456',
db_name,
charset='utf8') self.cursor = self.db.cursor() # init fake data
self.coll_policy_id = 1 # int
self.name = 'cisco'
self.cli_command = 'show run'
self.cli_command_result = 'good'
self.desc = '15s'
self.policy_type = 6 # int
self.snmp_oid = '1.1.1.1'
self.history = 'ok'
self.value_type = 9 # int
self.ostype_id = 10 # int def insert_data(self, table_name):
# insert into database
for t in range(0, 20):
sql = "INSERT INTO " + table_name + " (coll_policy_id, `name`, cli_command, cli_command_result, `desc`, policy_type, snmp_oid, history, value_type, ostype_id) VALUES"
# % (coll_policy_id, name, cli_command, cli_command_result, desc, policy_type, snmp_oid, history, value_type, ostype_id)
for i in range(1, 10000):
self.coll_policy_id = t*10000 + i
self.value_type = t*10000 + i
sql += "(" + `self.coll_policy_id` + "," + `self.name`+ "," + `self.cli_command` + "," + `self.cli_command_result` + "," + `self.desc` + "," + `self.policy_type` + "," + `self.snmp_oid` + "," + `self.history` + "," + `self.value_type` + "," + `self.ostype_id` + "),"
self.coll_policy_id = (t+1)*10000
self.value_type = (t+1)*10000
sql += "(" + `self.coll_policy_id` + "," + `self.name`+ "," + `self.cli_command` + "," + `self.cli_command_result` + "," + `self.desc` + "," + `self.policy_type` + "," + `self.snmp_oid` + "," + `self.history` + "," + `self.value_type` + "," + `self.ostype_id` + ")" print 'sql command created successfully..'
print sql try:
self.cursor.execute(sql)
self.db.commit()
print 'sql command executed successfully..'
except Exception,e:
self.db.rollback()
print 'database rollback..' + str(e)
# close database
self.db.close() print 'run successfully' if __name__ == '__main__':
inset_data = InsertData('apolo_test')
inset_data.insert_data('coll_policy') 在执行过程中由于sql语句中出现了一些关键字导致运行报错。这次是在关键字外用反引号(`)解决了这个问题。
`,repr(),str()都是将任意值转换成字符串类型的值。
`与repr()输出相同,方面机器阅读,str()输出则是方便人阅读。
例如:
>>> s = 'hi'
>>> print s
hi
>>> `s`
"'hi'"
>>> repr(s)
"'hi'"
>>> repr(1)
'1'
>>> str(s)
'hi'
>>> print `s`
'hi' >>> h = '\n'
>>> print h >>> print `h`
'\n'
>>> `h`
"'\\n'"

最新文章

  1. 【译】Import Changes from Direct3D 11 to Direct3D 12
  2. java8 ArrayList源码阅读
  3. Xcode常见错误汇总
  4. python 常用模块 Top200
  5. .net学习笔记---IIS 处理模型及ASP.NET页面生命周期
  6. Python开发【第二章】:Python的数据类型
  7. mysql EF
  8. 学习记录:浏览器JAVASCRIPT里的WINDOWS,DOCUMNET
  9. SQL Server内连接、外连接、交叉连接
  10. C语言初学 比较三个数中最大值的问题
  11. [置顶] 【cocos2d-x入门实战】微信飞机大战之十三:游戏场景过渡
  12. docker.service启动失败:Unit not found
  13. 【LeetCode】141. Linked List Cycle
  14. blob 和 sas
  15. 聊聊并发-Java中的Copy-On-Write容器
  16. Python基础之列表深浅复制和列表推导式
  17. Salesforce 导入导出数据简介
  18. spark连数据库
  19. Storm 入门教程
  20. href='#' 和 href='###'

热门文章

  1. c#之如何操作excel
  2. 国内老版本ubuntu更新源地址以及sources.list的配置方法
  3. Delphi文件目录操作(转载)
  4. Poj3624 Charm Bracelet (01背包)
  5. ftp权限设置大全!!!
  6. 第六节 DOM操作应用
  7. nginx与PHP配置
  8. bzoj4361 isn(树状数组优化dp+容斥)
  9. org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'null' to required type 'double' for property 'band'; nested exception is org.springframework.core.convert.Con
  10. 深度学习环境搭建:Tensorflow1.4.0+Ubuntu16.04+Python3.5+Cuda8.0+Cudnn6.0