python class 从数据库获取对应版本号

本文介绍如何使用Python类来连接数据库并查询特定版本号的方法,详细探讨了SQL查询和数据处理过程,适合初学者和进阶者提升Python数据库操作技能。
摘要由CSDN通过智能技术生成
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
import MySQLdb.cursors
import sys  
reload(sys) 
sys.setdefaultencoding( "utf-8" )

class Log:
    @staticmethod
    def e(msg):
        print(Log.red + '[x]:' + msg + Log.end)
    @staticmethod
    def w(msg):
        print(Log.yellow + '[!]:' + msg + Log.end)
    @staticmethod
    def i(msg):
        print(Log.green + '[+]:' + msg + Log.end)
    @staticmethod
    def t(msg):
        print(Log.blue + '[-]:' + msg + Log.end)
    red = '\033[1;31m'
    green = '\033[1;32m'
    yellow = '\033[1;33m'
    blue = '\033[1;34m'
    end = '\033[00m'

class get_marketcode(object):
    def connectdb(self):
        Log.w("start to connect database")  
        #本地连接 问号处填写数据库信息
        db = MySQLdb.connect(host='????', port=????, user='????', passwd='????', db='????', charset='latin1')
        Log.e("connect success")
        return db
    
    def select_table(self,table,db):
        cursor = db.cursor()
        sql='SELECT code,alias FROM '+table+' WHERE type in ("????")'   
        cursor.execute(sql)
        #打印数据
        Log.w("select database, results list:")
        results = cursor.fetchall()
        info=dict()
        if results:  
            for rec in results:  
                print rec[0],rec[1]
                info.update({rec[1]:rec[0]})
        Log.w("Get ALIAS:")
        for key,values in  info.items():
            print str(key)+"="+str(values)
        cursor.close()
        return info
    
    def invokedb(self):
        #连接数据库
        db = self.connectdb()
        product_table="customer_homepage"
        info=self.select_table(product_table,db)
        #关闭数据库
        db.close()
        return info
    
    def match(self,info,manufacturer):
        marketcode=info[manufacturer]
        Log.w("get marketcode of "+manufacturer+":")
        print manufacturer+"="+str(marketcode)
        return marketcode

if __name__ == '__main__':
    get_marketcode=get_marketcode()
    info=get_marketcode.invokedb()
    marketcode=get_marketcode.match(info,"tclshop")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值