Python语言 数据库处理
小标 2018-12-20 来源 : 阅读 1357 评论 0

摘要:本文主要向大家介绍了Python语言 数据库处理,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

本文主要向大家介绍了Python语言 数据库处理,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

# -*- coding:utf-8 -*-
#!/bin/env python
'''
#Auth:karl
#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]
    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, "root","****", 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", "*****", "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频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程