I'm trying to get a SQLite3 DB to populate a wx.ListCrtl. I can get it to print to stdout/stderr without any problem. I just can't seem to figure out how to display the data in the DataWindow/DataList? I'm still learning programing/python so any advice would be appreciated. After two days of trying to get it working, I've decided to consult the gurus.

database.py

import os
import sqlite3

class Database(object):
    
    def __init__(self, db_file="data/data.sqlite"):
        database_allready_exists = os.path.exists(db_file)
        self.db = sqlite3.connect(db_file)
        if not database_allready_exists:
            self.setupDefaultData()
            
    def select(self,sql):
        cursor = self.db.cursor()
        cursor.execute(sql)
        records = cursor.fetchall()
        cursor.close
        return records
    
    def insert(self,sql):
        newID = 0
        cursor = self.db.cursor()
        cursor.execute(sql)
        newID = cursor.lastrowid
        self.db.commit()
        cursor.close()
        return newID
    
    def save(self,sql):
        cursor = self.db.cursor()
        cursor.execute(sql)
        self.db.commit()
        cursor.close()
        
    def setupDefaultData(self):
        pass

Main.py

import wx
import wx.lib.mixins.listctrl  as  listmix
from database import *
import sys
        
class DataWindow(wx.Frame):
    def __init__(self, parent = None):
        wx.Frame.__init__(self, parent, -1, 'DataList', size=(640,480))
        self.win = DataList(self)
        self.Center()
        self.Show(True)
        
class DataList(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.ColumnSorterMixin):
    def __init__(self, parent = DataWindow):
        wx.ListCtrl.__init__( self, parent, -1, style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES)

        #building the columns
        self.InsertColumn(0, "Location")
        self.InsertColumn(1, "Address")
        self.InsertColumn(2, "Subnet")
        self.InsertColumn(3, "Gateway")
        self.SetColumnWidth(0, 100)
        self.SetColumnWidth(1, 150)
        self.SetColumnWidth(2, 150)
        self.SetColumnWidth(3, 150)

class MainWindow(wx.Frame):
    def __init__(self, parent = None, id = -1, title = "MainWindow"):
        wx.Frame.__init__(self, parent, id, title, size = (800,600),
                          style = wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER))
        
        # StatusBar
        self.CreateStatusBar()
        
        # Filemenu
        filemenu = wx.Menu()
        
        # Filemenu - About
        menuitem = filemenu.Append(-1, "&About", "Information about this application")
        self.Bind(wx.EVT_MENU, self.onAbout, menuitem)
        
        #Filemenu - Data
        menuitem = filemenu.Append(-1, "&Data", "Get data")
        self.Bind(wx.EVT_MENU, self.onData, menuitem)
        # Filemenu - Seperator
        filemenu.AppendSeparator()
        
        #Filemenu - Exit
        menuitem = filemenu.Append(-1, "&Exit", "Exit the application")
        self.Bind(wx.EVT_MENU, self.onExit, menuitem)
        
        # Menubar
        menubar = wx.MenuBar()
        menubar.Append(filemenu, "&File")
        self.SetMenuBar(menubar)
        
        # Show
        self.Show(True)
        self.Center()
        
    def onAbout(self, event):
        pass
        
    def onData(self, event):
        DataWindow(self)
        db = Database()
        sql = "SELECT rowid, address, subnet, gateway FROM pod1"
        records = db.select(sql)
        count = 0
        for v in records:
            index = DataList.InsertStringItem(sys.maxint, str(count + 1))
            DataList.SetStringItem(index, 1, v['address'])
            DataList.SetStringItem(index, 2, v['subnet'])
            DataList.SetStringItem(index, 3, v['gateway'])
            count += 1

        
    def onExit(self, event):
        self.Close()
        self.Destroy()
        
    def onSave(self, event):
        pass
        
if __name__ == '__main__':
    app = wx.App()
    frame = MainWindow(None, -1)
    frame.Show()
    app.MainLoop()

Edited 6 Years Ago by dunwitch: n/a

Not tested but I have problem with your OnData Methods
Please try this and see what it gives.

def onData(self, event):
        DataWindow(self)
        db = Database()
        sql = "SELECT rowid, address, subnet, gateway FROM pod1"
        records = db.select(sql)
        count = 0
        for v in records:
            index = DataList.InsertStringItem(sys.maxint, str(count + 1))
            DataList.SetStringItem(index, 1, v[0])
            DataList.SetStringItem(index, 2, v[1])
            DataList.SetStringItem(index, 3, v[2])
            count += 1

This should help you a lot in your work. All the best

This article has been dead for over six months. Start a new discussion instead.