功能介绍:
查询数据库表数据,提取数据并显示
main.py(执行文件)
#coding:gbk __author__ = 'Hito' import querySmscode import wx class MyFrame( wx.Frame ): def __init__( self): wx.Frame.__init__(self,None,-1, 'SMSCODE',(-1,-1),wx.Size(350,300)) code=querySmscode.querySmscode() panel = wx.Panel(self,-1) smsCodeBox = wx.BoxSizer( wx.HORIZONTAL ) head=wx.StaticText(panel, 1,"Phone\t\tSmscode",(120,10),(200, -1), wx.ALIGN_CENTER) self.content=wx.TextCtrl(panel, 2,code,(120,35),(300, -1), style = wx.TE_MULTILINE|wx.TE_READONLY) self.Refresh = wx.Button(panel,3,u"查询") wx.EVT_BUTTON(self,3,self.show_Smscode) sizer = wx.FlexGridSizer(rows=3,cols=1, hgap=6, vgap=6) sizer.AddMany([(head,0,wx.ALIGN_CENTER), (self.content,0,wx.ALIGN_CENTER),(self.Refresh,0,wx.ALIGN_CENTER)]) smsCodeBox.Add(sizer, proportion=1, flag=wx.ALL|wx.EXPAND, border=15) panel.SetSizer(smsCodeBox) self.Center() def show_Smscode(self,event): self.Refresh.SetLabel(u"查询中...") code=querySmscode.querySmscode() self.content.SetValue(code) self.Refresh.SetLabel(u"查询") if __name__ == "__main__": app = wx.App() frame = MyFrame() frame.Show() app.MainLoop()
querySmscode.py
#coding:gbk
__author__ = 'yanghaitao'
import pymssql
import ConfigParser,os
cf = ConfigParser.ConfigParser()
cf.read('./smscode.ini')
HOST=cf.get("DB_CONFIG","host")
DATABASE=cf.get("DB_CONFIG","database")
USER=cf.get("DB_CONFIG","user")
PASSWORD=cf.get("DB_CONFIG","password")
def querySmscode(n=20):
result=''
count=0
sql='SELECT TOP %d * FROM SMScode ORDER BY Id DESC '%n
result_list=getData(sql)
for i in range(n):
if str(result_list[i][3]) != 'None' and count<10 and len(str(result_list[i][3])) == 4:
count=count+1
result=result + str(result_list[i][1])+'\t'+str(result_list[i][3])+'\t'+str(result_list[i][2])[0:19]+'\n'
return result
def getData(sql): #执行数据库查询函数
conn=pymssql.connect(host=HOST,user=USER,password=PASSWORD,database=DATABASE,charset="UTF-8")
cou=conn.cursor()
cou.execute(sql)
result = cou.fetchall()
conn.commit()
conn.close()
return result
smscode.ini(数据库配置文件,具体的读写操作,可参考http://www.cnblogs.com/hito/p/4651647.html)
[DB_CONFIG]
host = XXX.XXX.X.X
database = 数据库名
user = 账号
password = 密码