Python小程序,读取ACCESS数据库,然后list数据

曾经做过的一个Python小程序,读取ACCESS数据库,然后list数据

# -*- coding: cp936;
import wx
import wx.lib
import sys,glob,random
import win32com.client
reload(sys)
sys.setdefaultencoding('utf-8')
conn = win32com.client.Dispatch(r'ADODB.Connection')
rs = win32com.client.Dispatch(r'ADODB.Recordset') 
class Search(wx.Frame):
    def __init__(self,text1,text3):
        wx.Frame.__init__(self,None,-1,u"安全检查",size=(800,600))
        
        def adddate():
            DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=yb.mdb;'
            conn.Open(DSN)          
            rs_name ='aqjc'
            sql='Select * FROM [aqjc] where 1=1'
            if text1!='':
                sql=sql+'and id = '+text1
            else:sql=sql+'and job like \'%'+text3+'%\''
            rs.Open(sql,conn,1,3)
            
            
            self.list.InsertColumn(0,u'备注',format=wx.LIST_FORMAT_CENTRE, width=-1)
            self.list.InsertColumn(0,u'处置',format=wx.LIST_FORMAT_CENTRE, width=-1)
            self.list.InsertColumn(0,u'发现问题',format=wx.LIST_FORMAT_CENTRE, width=-1)
            self.list.InsertColumn(0,u'时段',format=wx.LIST_FORMAT_CENTRE, width=-1)
            self.list.InsertColumn(0,u'时间',format=wx.LIST_FORMAT_CENTRE, width=-1)
            self.list.InsertColumn(0,u'job',format=wx.LIST_FORMAT_CENTRE, width=-1)
            self.list.InsertColumn(0,u'riqi',format=wx.LIST_FORMAT_CENTRE, width=-1)
            self.list.InsertColumn(0,u'id',format=wx.LIST_FORMAT_CENTRE, width=-1)

            while not rs.EOF:
                index = self.list.InsertStringItem(sys.maxint,str(rs.Fields.Item(0).value))
                self.list.SetStringItem(index,1,str(rs.Fields.Item(1).value))
                self.list.SetStringItem(index,2,str(rs.Fields.Item(2).value).decode('utf-8').encode('gbk'))
                self.list.SetStringItem(index,3,str(rs.Fields.Item(3).value).decode('utf-8').encode('gbk'))
                self.list.SetStringItem(index,4,str(rs.Fields.Item(4).value))
                self.list.SetStringItem(index,5,str(rs.Fields.Item(5).value).decode('utf-8').encode('gbk'))
                self.list.SetStringItem(index,6,str(rs.Fields.Item(6).value).decode('utf-8').encode('gbk'))
                self.list.SetStringItem(index,7,str(rs.Fields.Item(7).value).decode('utf-8').encode('gbk'))
                rs.MoveNext()
            self.list.Bind(wx.EVT_LIST_ITEM_ACTIVATED,self.OnDclick)
            rs.Close()
            conn.Close()
        
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        panel = wx.Panel(self,-1)
        self.list = wx.ListCtrl(self, -1, style=wx.LC_REPORT|wx.LC_EDIT_LABELS,size=(800,600))
        self.list.SetSingleStyle(wx.LC_REPORT|wx.LC_HRULES|wx.LC_VRULES,True,)
        hbox.Add(self.list,1,1)
        panel.SetSizer(hbox)
        self.SetAutoLayout(1)
        hbox.Fit(self)
        adddate()
    def OnDclick(self,event):
        content=event.GetText()
        frame=UpDateFrame(content)
        frame.Show()
class UpDateFrame(wx.Frame):
    def __init__(self,content):
        wx.Frame.__init__(self,None,-1,u"更改数据",size=(800,800))
        DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=yb.mdb;'
        conn.Open(DSN)
        sql='Select * FROM [aqjc] where id='+content
        rs.Open(sql,conn,1,3)
        rs.MoveFirst()
        
        text1=str(rs.Fields.Item(1).value)
        text2=str(rs.Fields.Item(2).value).decode('utf-8').encode('gbk')
        text3=str(rs.Fields.Item(3).value).decode('utf-8').encode('gbk')
        text4=str(rs.Fields.Item(4).value)
        text5=str(rs.Fields.Item(5).value).decode('utf-8').encode('gbk')
        text6=str(rs.Fields.Item(6).value).decode('utf-8').encode('gbk')
        text7=str(rs.Fields.Item(7).value).decode('utf-8').encode('gbk')
           
        panel = wx.Panel(self)
        riqiLbl = wx.StaticText(panel, -1, "日期:")
        self.riqi = wx.TextCtrl(panel, -1, ""+text1);
        gangweiLbl = wx.StaticText(panel, -1, "岗位:")
        self.gangwei= wx.TextCtrl(panel, -1, ""+text2);
        shijianLbl = wx.StaticText(panel, -1, "时间:")
        self.shijian = wx.TextCtrl(panel, -1, ""+text3);
        shiduanLbl = wx.StaticText(panel, -1, "时段:")
        self.shiduan = wx.TextCtrl(panel, -1, ""+text4);
        wentiLbl = wx.StaticText(panel, -1, "发现问题:")
        self.wenti = wx.TextCtrl(panel, -1, ""+text5);
        chuzhiLbl = wx.StaticText(panel, -1, "处置:")
        self.chuzhi = wx.TextCtrl(panel, -1, ""+text6);
        beizhuLbl = wx.StaticText(panel, -1, "备注:")
        self.beizhu = wx.TextCtrl(panel, -1, ""+text7);

        mainSizer = wx.BoxSizer(wx.VERTICAL)

        riqiSizer=wx.BoxSizer(wx.HORIZONTAL)
        riqiSizer.Add(riqiLbl, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        riqiSizer.Add(self.riqi, 0, wx.EXPAND)

        gangweiSizer=wx.BoxSizer(wx.HORIZONTAL)
        gangweiSizer.Add(gangweiLbl, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        gangweiSizer.Add(self.gangwei, 0, wx.EXPAND)

        shijianSizer=wx.BoxSizer(wx.HORIZONTAL)
        shijianSizer.Add(shijianLbl, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        shijianSizer.Add(self.shijian, 0, wx.EXPAND)

        shiduanSizer=wx.BoxSizer(wx.HORIZONTAL)
        shiduanSizer.Add(shiduanLbl, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        shiduanSizer.Add(self.shiduan, 0, wx.EXPAND)

        chuzhiSizer=wx.BoxSizer(wx.HORIZONTAL)
        chuzhiSizer.Add(chuzhiLbl, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        chuzhiSizer.Add(self.chuzhi, 0, wx.EXPAND)

        wentiSizer=wx.BoxSizer(wx.HORIZONTAL)
        wentiSizer.Add(wentiLbl, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        wentiSizer.Add(self.wenti, 0, wx.EXPAND)

        beizhuSizer=wx.BoxSizer(wx.HORIZONTAL)
        beizhuSizer.Add(beizhuLbl, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        beizhuSizer.Add(self.beizhu, 0, wx.EXPAND)

        Btn = wx.Button(panel, -1, "确认提交")
        self.Bind(wx.EVT_BUTTON,self.OnClick,Btn)
        
        mainSizer.Add(riqiSizer,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        mainSizer.Add(gangweiSizer,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        mainSizer.Add(shijianSizer,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        mainSizer.Add(shiduanSizer,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        mainSizer.Add(wentiSizer,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        mainSizer.Add(chuzhiSizer,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        mainSizer.Add(beizhuSizer,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        mainSizer.Add(wx.StaticLine(panel), 0,wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
        mainSizer.Add(Btn,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)        

        panel.SetSizer(mainSizer)
        mainSizer.Fit(self)
    def OnClick(self,event):
        
        box=wx.MessageDialog(None,'确定更新?','提交更新',wx.OK)  
        answer=box.ShowModal()  
        box.Destroy()
        
class DemoFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,u"安全检查",size=(800,800))
        panel = wx.Panel(self)

        searchLbl1 = wx.StaticText(panel, -1, "id:")
        self.search1 = wx.TextCtrl(panel, -1, "");

        searchLbl3 = wx.StaticText(panel, -1, "岗位:")
        self.search3 = wx.TextCtrl(panel, -1, "");
        searchBtn3 = wx.Button(panel, -1, "查询")
        self.Bind(wx.EVT_BUTTON,self.OnClick3,searchBtn3)
        
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        searchSizer3=wx.BoxSizer(wx.HORIZONTAL)
        searchSizer3.Add(searchLbl1, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        searchSizer3.Add(self.search1, 0, wx.EXPAND)
        searchSizer3.Add(searchLbl3, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        searchSizer3.Add(self.search3, 0, wx.EXPAND)
        searchSizer3.Add(searchBtn3, 0, wx.EXPAND)

        mainSizer.Add(searchSizer3,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        mainSizer.Add(wx.StaticLine(panel), 0,wx.EXPAND|wx.TOP|wx.BOTTOM, 5)

        panel.SetSizer(mainSizer)
        mainSizer.Fit(self)

    def OnClick3(self,event):
        text1=self.search1.GetValue()
        text3=self.search3.GetValue()
        frame = Search(text1,text3)        
        frame.Show(True)
        return True
    
if __name__=='__main__':
    app = wx.PySimpleApp()
    frame1 = DemoFrame()
    frame1.Show()
    app.MainLoop()

 

posted @ 2014-08-11 11:07  竹花小米  阅读(926)  评论(0编辑  收藏  举报