从Excel中读取数据并批量写入MySQL数据库(基于pymysql)
一、Excel内容时这样的:

二、最初的代码是这样的:
# -*- coding:utf-8 -*-
import pymysql
from xlrd import open_workbook class DB:
global host,username,password,port,database,config
host = "xx.xx.xx.xxx"
username = "root"
password = "xxxx"
port = 3306
database = "pythondb"
config = {
'host': str(host),
'user': username,
'passwd': password,
'port': int(port),
'db': database
}
def __init__(self):
self.db = None
self.cursor = None def connectDB(self):
try:
self.db = pymysql.connect(**config)
self.cursor = self.db.cursor()
print "Connect DB successfully!"
except:
print "Connect DB failed!" def executeSQL(self,sql):
self.connectDB()
self.cursor.execute(sql)
self.db.commit()
return self.cursor def getAll(self,cursor):
value = cursor.fetchall()
return value def getOne(self,cursor):
value = cursor.fetchone()
return value def closeDB(self):
self.db.close()
print "Database closed!" def get_xls(self,xls_name, sheet_name):
"""
get interface data from xls file
:return:
"""
cls = []
# open xls file
file = open_workbook(xls_name)
# get sheet by name
sheet = file.sheet_by_name(sheet_name)
# get one sheet's rows
nrows = sheet.nrows
for i in range(1,nrows):
cls.append(sheet.row_values(i))
return tuple(cls) if __name__ == "__main__":
mysqlInfo = DB()
content = mysqlInfo.get_xls("DataSource.xls","Sheet1")
for i in range(0,len(content)):
id = content[i][0]
begin_date = str(content[i][1])
end_date = content[i][2]
in_num = content[i][3]
out_num = content[i][4]
all_num = content[i][5]
create_time = content[i][6]
use_time = content[i][7]
scope_date = content[i][8]
sql = "INSERT INTO `day`( ID, BEGIN_DATE, END_DATE, IN_NUM, OUT_NUM, ALL_NUM, CREATE_TIME, USE_TIME, SCOPE_DATE ) VALUES(%s,'%s','%s',%s,%s,%s,'%s',%s,%s)" %(id, begin_date, end_date, in_num, out_num, all_num, create_time, use_time, scope_date)
mysqlInfo.executeSQL(sql)
mysqlInfo.closeDB()
从Excel中读取数据并批量写入MySQL数据库(基于pymysql)的更多相关文章
- 从Excel中读取数据并批量写入MySQL数据库(基于MySQLdb)
一.Excel内容如下,现在需要将Excel中的数据全部写入的MySQL数据库中: 二.连接MySQL的第三方库使用的是“MySQLdb”,代码如下: # -*- coding:utf-8 -*-im ...
- php从memcache读取数据再批量写入mysql的方法
这篇文章主要介绍了php从memcache读取数据再批量写入mysql的方法,可利用memcache缓解服务器读写压力,并实现数据库数据的写入操作,非常具有实用价值,需要的朋友可以参考下. 用 Mem ...
- 用python在excel中读取与生成随机数写入excel中
今天是我第一次发博客,就关于python在excel中的应用作为我的第一篇吧. 具体要求是:在一份已知的excel表格中读取学生的学号与姓名,再将这些数据放到新的excel表中的第一列与第二列,最后再 ...
- 从Excel中读取数据(python-xlrd)
从Excel中读取数据(python-xlrd) 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls ...
- 在实现从excel中读取数据作为接口参数遇到的问题
这个算我自己第一次使用python语言实现 一个功能 一.首先我们先要代码实现如何从excel上读取数据python实现还是比较简单的 1.我使用的是xlrd模块,我们先要安装这个包,这样我们才可以使 ...
- PHP批量替换MySql数据库中的数据内容(替换MySql数据库内容源码)
PHP批量替换MySql数据库内容 UTF-8 1.0版 <?php //声明 //1.本源码开发意图:作者在使用一些CMS建站的时候发现很多CMS把网址写入到数据库了,如果换网址,那么就需要更 ...
- Python读取Excel中的数据并导入到MySQL
""" 功能:将Excel数据导入到MySQL数据库 """ import xlrd import MySQLdb # Open the w ...
- Badboy - 从excel中读取数据
参考: http://leafwf.blog.51cto.com/872759/1119161 http://www.51testing.com/html/00/130600-1367743.html ...
- C#从Excel中读取数据为空
将HDR设置为YES,IMEX设置为1即可. OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.ACE.OL ...
随机推荐
- twig 模板控制器对应列表
{{ render(controller(metas.header,{request:app.request, course: course, member: member|default(null) ...
- [LuoguP3064][USACO12DEC]伊斯坦布尔的帮派Gangs of Istanbull(加强版)_线段树_贪心
伊斯坦布尔的帮派Gangs of Istanbull 题目链接:https://www.luogu.org/problem/P3064 数据范围:略. 题解: 这个题其实分为两问,第一问是$YES$. ...
- [转帖]【Oracle】详解Oracle中NLS_LANG变量的使用
[Oracle]详解Oracle中NLS_LANG变量的使用 https://www.cnblogs.com/HDK2016/p/6880560.html NLS_LANG=LANGUAGE_TERR ...
- Sumitomo Mitsui Trust Bank Programming Contest 2019 Task F. Interval Running
Link. There is a nice approach to this problem that involves some physical insight. In the following ...
- Reactor 线程模型以及在netty中的应用
这里我们需要理解的一点是Reactor线程模型是基于同步非阻塞IO实现的.对于异步非阻塞IO的实现是Proactor模型. 一 Reactor 单线程模型 Reactor单线程模型就是指所有的IO操作 ...
- NOIP2012 借教室 题解 洛谷P1083
一看就是暴力 好吧,其实是线段树或差分+二分,这里用的是差分+二分的做法. 二分部分的代码,套个二分板子就行 ,right=m; while(left<right)//二分 { ; ; else ...
- docker-compose搭建elasticsearch+kibana环境,以及php使用elasticsearch
一.elasticsearch的Dockerfile 增加中文搜索插件analysis-ik FROM docker.elastic.co/elasticsearch/elasticsearch:7. ...
- F12的用法
F12在Web测试中十分重要,可以定位元素(UI自动化常用),查看网页响应时间/数据(定位BUG,测单页面响应时间→性能) Elements 点击这个按钮,将光标移至“Google”图片位置并点击,右 ...
- outlook邮箱备份
- nginx日志模块、事件模块
日志模块 1.access_log指令 语法: access_log path [format [buffer=size [flush=time]]]; access_log logs/access. ...