Hi, i am working on program that connect to database, and user then cans see, edit, insert and delete data in mysql database. I have problem whit update query, can somone help me whit this. User need to click on cell and enter/change data, then hit enter to save changes.

import wx
import wx.grid
import MySQLdb

class SimpleGrid(wx.grid.Grid):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.currentlySelectedCell = (0, 0)

        self.myGrid=wx.grid.Grid(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.myGrid, 1, wx.EXPAND)
        self.SetSizer(sizer)

        self.myGrid.Bind(wx.EVT_KEY_DOWN, self.onKeyPress)
        self.myGrid.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.onSingleSelect)

        self.db = MySQLdb.connect("127.0.0.1", "usename", "password", "database")
        self.cursor = self.db.cursor()
        self.display()

    def display(self):
        data=(self.getData())

        nrw=len(data)
        ncl=len(data[0])
        self.nrw=nrw
        self.myGrid.CreateGrid(nrw+1, ncl)

        self.myGrid.SetColLabelValue(0, "idSetup")
        self.myGrid.SetColLabelValue(1, "idKomitent")
        self.myGrid.SetColLabelValue(2, "Drzava")
        self.myGrid.SetColLabelValue(3, "Prg")
        self.myGrid.SetColLabelValue(4, "Provajder")
        self.myGrid.SetColLabelValue(5, "ServerName")
        self.myGrid.SetColLabelValue(6, "DataBaseName")
        self.myGrid.SetColLabelValue(7, "Sifarnici")
        self.myGrid.SetColLabelValue(8, "Promena")
        for rw in range(0,nrw):
            for cl in range(0,ncl):
                self.myGrid.SetCellValue(rw, cl,str(data[rw][cl]))

        #print self.GetCellPosition(0,0)

    def insert(self):
        rw=tuple()
        for a in range(0,9):
            rw+=(self.myGrid.GetCellValue(int(self.currentlySelectedCell[0]),a),)

        query = #?? can somone help me whit this.
        self.cursor.execute(query)
        self.db.commit()
        self.myGrid.Destroy()

    def update(self):
        rw=tuple()
        print (self.currentlySelectedCell)
        for a in range(0,9):
            rw+=(self.myGrid.GetCellValue(int(self.currentlySelectedCell[0]),a),)

        query = "update setup values('%s')"%rw[0]
        self.cursor.execute(query)
        self.db.commit()
        self.myGrid.Destroy()

    def delete(self):
        rw=tuple()

        for a in range(0,9):
            rw+=(self.myGrid.GetCellValue(int(self.currentlySelectedCell[0]),a),)

        query="delete from setup where idSetup='%s'"%rw[0]
        self.cursor.execute(query)
        self.db.commit()
        self.myGrid.Destroy()

    def ShowMessage(self,x,y):
        wx.MessageBox(y, x, 
            wx.OK | wx.ICON_INFORMATION)

    def getData(self):

        query = "SELECT * FROM setup"
        self.cursor.execute(query)
        data = []
        res = self.cursor.fetchall()
        for i in res:
            data.append(i)
        return data

    def onSingleSelect(self, event):
        """
        Get the selection of a single cell by clicking or 
        moving the selection with the arrow keys
        """
        #print "You selected Row %s, Col %s" % (event.GetRow(),
        #                                         event.GetCol())
        self.currentlySelectedCell = (event.GetRow(),
                                      event.GetCol())

        event.Skip()

    def onKeyPress(self, event):
        keycode = event.GetKeyCode()

        if keycode == 13:
            if self.currentlySelectedCell[0]+1>self.nrw:
                self.insert()

            else:
                self.update()
        elif keycode == 127:
            self.delete()
        elif keycode==342:

            self.myGrid.SetFocus() 
            self.myGrid.SelectRow(self.currentlySelectedCell[0]) 

        event.Skip()

class TestFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "A Grid",
                size=(900, 275))
        grid = SimpleGrid(self)
        self.CenterOnScreen()

app = wx.App()
frame = TestFrame(None)
frame.Show()
app.MainLoop()

i am using Python 2.7, and WX version 3.0.2

What is the problem? Please do not just reply "it doesn't work". You wouldn't take your car to a mechanic and tell them only "it doesn't work".

commented: Actually does happen. The owner employs other people to take care of such things. Companies call these folk employees. +14

I dont no how to setup update query. Whole this part is not functioning well. When I edit cell and hit enter nothing hapends.

def update(self):
        rw=tuple()
        print (self.currentlySelectedCell)
        for a in range(0,9):
            rw+=(self.myGrid.GetCellValue(int(self.currentlySelectedCell[0]),a),)
        query = "update setup values('%s')"%rw[0]
        self.cursor.execute(query)
        self.db.commit()
        self.myGrid.Destroy()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.