wxPython MVC 数据库小demo

#Model---------------------
'''
Created on 2012-3-22

@author: caoZhongping

this class is Model for MVC
'''
import wx.grid

class GenericTable(wx.grid.PyGridTableBase):
        
        def __init__(self,data,rowLabels=None,colLabels=None):
                wx.grid.PyGridTableBase.__init__(self)
                self.data = data
                self.rowLabels = rowLabels
                self.colLabels = colLabels
                
        def GetNumberRows(self):
            return len(self.data)  
             
        def GetNumberCols(self):
            return len(self.data[0])
        
        def GetColLabelValue(self,col):
            if self.colLabels:
                return self.colLabels[col]
            
        def GetRowLabelValue(self,row):
            if self.rowLabels:
                return self.rowLabels[row]
            
        def IsEmptyCell(self,row,col):
            return False    
        
        def GetValue(self,row,col):
            return self.data[row][col]    
        
        def SetVaule(self,row,col,value):
            pass             
#VIEW AND contrler
#coding:GBK
'''
Created on 2012-3-22

@author: cWX53049
'''
import wx.grid
import GenericTable
import SqlUtil

colLabel=("ID","版本号","类型","版本ID","显示名称")
frameTitle="查询结果"


class SimpleGrid(wx.grid.Grid):


    def __init__(self,parnet):
        
        wx.grid.Grid.__init__(self,parnet,-1)
        data = SqlUtil.queryData('select * from pm_tbl_VersionMap');
        rowLabel = []
        for i in range(len(data)):
            rowLabel.append(i)
        self.baseModel = GenericTable.GenericTable(data,rowLabel,colLabel)
        self.SetTable(self.baseModel)
    def GetModel(self):
        return  self.baseModel  
class TestFrame(wx.Frame):
    
    def __init__(self,parent):
        wx.Frame.__init__(self,parent,-1,frameTitle,size=(400,300))
        SimpleGrid(self)
        
if __name__=='__main__':
    app = wx.PySimpleApp()
    test = TestFrame(None)
    test.Center()     
    test.Show()   
    app.MainLoop()
        
        

# DBUtil-----------


import MySQLdb
    
dbHost = '10.148.136.61'
userName = 'system'
pwd = 'sql' 
databaseStr = 'T123'
charsetStr = 'gbk'

def getConn():
    return  MySQLdb.connect(host=dbHost, user=userName, passwd=pwd, db=databaseStr, charset=charsetStr) 
def queryData(sql):
    try: 
        conn = getConn()   
        cursor = conn.cursor()
        cursor.execute(sql)
#        print cursor.fetchall()
#        for row in cursor.fetchall():
#            print unicode(row)
        return cursor.fetchall()    
    except Exception, e:
        print e
        return - 1
    finally:
        cursor.close()
        conn.close() 
def createOrInitTable(sql):
    try: 
        conn = getConn()   
        cursor = conn.cursor()
        n = cursor.execute(sql)
        print 'Execute SQL return ', n
    except Exception, e:
        print e
    finally:
        cursor.close()
        conn.close() 
if __name__ == '__main__':
    data = queryData('select * from pm_tbl_VersionMap')
    print data

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值