#!/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")