# -*- coding: UTF-8 -*- import time import MySQLdb hostUrl = "139.199.179.128" hortPort = 19000 username = " " password = " " db_center = "guangxi_center" db_logic = "guangxi_logic" gset =set() conn = MySQLdb.connect( host=hostUrl, port=hortPort, user=username, passwd=password ) cur = conn.cursor() cur.execute( "select version()" ) data = cur.fetchone() print "MySQL-Server Version : %s" % data print "----------------------------------------------------------------\n" #获取 今天0点的时间 def GetToday0DianUnixTime(): cur_time = time.time() return int(cur_time-cur_time%86400 - 8*3600) def GetYesday0DianUnixTime(): cur_time = time.time() return int(cur_time-86400-cur_time%86400 - 8*3600) def AddMapVal(Map,Key,Val): if Map.has_key(Key): Map[Key] = Map[Key]+Val else: Map[Key] = Val g_StartUnixTIme=GetYesday0DianUnixTime() g_EndUnixTIme=g_StartUnixTIme+86400 #select id, sum(cardnumber) as ddddd from charge where time < 333 and time >33333 group by id order by ddddd; print "QuerStart : "+ str(g_StartUnixTIme) print "QuerEnd : "+ str(g_EndUnixTIme) conn.select_db( db_center ) cur.execute( "select Userid,CardType from charge where type =3 and time >%d and time <%d" % (g_StartUnixTIme,g_EndUnixTIme)) results = cur.fetchall() # CARD_TYPE_8 = 2,//8局卡 # CARD_TYPE_16 = 3,//16局卡 UserFlag = {} def AddUserFlag(ID, n): if UserFlag.has_key(ID): UserFlag[ID] = UserFlag[ID]+n else: UserFlag[ID] = n countAry=[0 for i in range (400)] for row in results: AddUserFlag(row[0],row[1]-1) for Key,Val in UserFlag.items(): set.insert() countAry[Val] = countAry[Val]+ 1 if Val >=60 and Val <100: print "key:"+str(Key)+",value:"+str(Val) for Val in range(0,400): if countAry[Val] >0: print Val , countAry[Val] with open("resludete.txt", 'w') as f: for Key,Val in UserFlag.items(): if Val >=2 and Val <10: f.writelines("%d\t%d\n"%(Key,Val)) for Val in range(0,400): if countAry[Val] >0: f.writelines("%d\t%d\n"%(Val,countAry[Val])) # time.sleep(33) """ 关闭数据库连接 """ conn.close() raw_input( "按回车键退出...".decode( 'utf-8' ).encode( 'gbk' ) )
Python数据库查询
最新推荐文章于 2024-07-08 21:15:10 发布