首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
数据库
> 详细
python 数据库处理
时间:
2017-11-29 10:23:38
阅读:
181
评论:
0
收藏:
0
[点我收藏+]
标签:
python
mysql
excel
# -*- coding:utf-8 -*- #!/bin/env python ''' #Auth: tyk #Function: released version #Date:2017/6/27 #Version:V1.0 ''' import sys,re,time,datetime import paramiko import logging import ConfigParser import traceback import MySQLdb import platform import xlrd reload(sys) sys.setdefaultencoding('utf-8') if "Linux" == platform.system(): Excelpath="/home/appdeploy/version/Version" else: Excelpath = "D:\\auto\\release\Version" class Mysql_connect(object): def __init__(self,*agre): self.port=agre[0] self.passwd=agre[1] self.username=agre[2] self.host=agre[3] self.version=agre[4] pass def File_get(self): Excelfilepath="{path}_{version}/Filelist.xlsx".format(path=Excelpath,version=self.version) workbook=xlrd.open_workbook(Excelfilepath) booksheet = workbook.sheet_by_name('Sheet1') self.p=list() for row in range(booksheet.nrows): row_data = [] for col in range(booksheet.ncols): cel = booksheet.cell(row, col) val = cel.value try: val = cel.value val = re.sub(r'\s+', '', val) except: pass if type(val) == float: val = int(val) else: val = str(val) row_data.append(val) self.p.append(row_data) self.__Mysql_deal() def __Mysql_deal(self): try: con = MySQLdb.connect(user=self.username,passwd=self.passwd,db="version_release",port=int(self.port),charset="utf8") dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") cursor=con.cursor() cursor.execute("CREATE TABLE IF NOT EXISTS version(Id INT PRIMARY KEY AUTO_INCREMENT, File VARCHAR (30) DEFAULT null,Release_date TIMESTAMP,Instruction text)") sql="""INSERT INTO version (File,Release_date,Instruction) VALUES (%s, %s, %s) """ for value in self.p: valuse=(value[0],dt,value[1]) result=cursor.execute(sql,valuse) cursor.close() con.commit() con.close() except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) sys.exit(1) def File_deal(self): try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self.host,22, "appdeploy","sf123456", timeout=5) for name in self.p: route_0=name[0] route=name[1] cmd="\cp -vr {path}_{version}/{filename}.xls {dir_new}".format(path=Excelpath,version=self.version,filename=route_0,dir_new=route) stdin, stdout, stderr = ssh.exec_command(cmd) for out_msg in stdout.readlines(): print out_msg except Exception, e: print '%s\tError\n' % (self.host) ,e ssh.close() def main(): agre = ["3306", "Huawei12#$", "root","xx.xx.xx.xx","3.0"] stg = Mysql_connect(*agre) stg.File_get() stg.File_deal() if __name__ == '__main__': try: main() except Exception as e: print traceback.format_exc()
python 数据库处理
标签:
python
mysql
excel
原文地址:http://blog.51cto.com/12768454/2045516
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
数据库进阶
2021-07-29
在 Oracle 数据库中执行 SQL 语句遇到特殊字符的转义方式
2021-07-28
Windows Logstash同步 Sqlserver 到Elasticsearch
2021-07-26
mysql数据库(11):恢复数据
2021-07-26
mysql数据库(9):常用查询的例子
2021-07-26
SQLAlchemy 多对多
2021-07-26
ClickHouse的JDBC连接
2021-07-26
Apache HBase 1.7.1 发布,分布式数据库
2021-07-26
数据库常用架构和同步工作原理
2021-07-26
MySQL数据库设计规范(仅供参考)
2021-07-26
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!