Hi, I'm new to Python, I'm practicing queries sqlite3 database, I will present what I have so far.

until now I have not seen an example of how to reflect queries a database using wxPython

I hope I help, do not write very good English.

frame.py

import wx
import sqlite3 as dbapi

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        MyPanel(self)

class MyPanel(wx.Panel):
    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)

        inventario = wx.StaticText(self, -1, u'Bienvenido Inventario: ', pos = (35,10))
        codigo = wx.StaticText(self, -1, 'Codígo: ', pos = (20,30))
        self.codigo_cuadro = wx.TextCtrl(self, -1, '', pos = (20,50))
        producto = wx.StaticText(self, -1, 'Producto: ', pos = (20,80))
        self.producto_cuadro = wx.TextCtrl(self, -1, '', pos = (20,100))
        costo = wx.StaticText(self, -1, 'Costo: ', pos = (20,130))     
        self.costo_cuadro = wx.TextCtrl(self, -1, '', pos = (20,150))
        guardar = wx.Button(self, -1, 'Guardar', pos = (20,180))
        buscar = wx.Button(self, -1, 'Buscar', pos = (20,210))
        salir = wx.Button(self, -1, 'Salir', pos = (20,240))

        guardar.Bind(wx.EVT_BUTTON, self.OnGuardar)
        buscar.Bind(wx.EVT_BUTTON, self.OnBuscar)
        salir.Bind(wx.EVT_BUTTON, self.OnSalir)

    def OnGuardar(self, evt):
        bd = dbapi.connect("formulario.dat")
        cursor = bd.cursor()
        cursor.execute("""create table if not exists inventario (codigo_cuadro txt, producto_cuadro txt, costo_cuadro txt)""")
        bd.commit()
        cursor.close()
        bd.close()
        ingresodecodigo = self.codigo_cuadro.GetValue()
        dialogo = wx.MessageDialog(self, 'El producto %s, se ha guardado correctamente' % (ingresodecodigo), 'Información', wx.OK | wx.ICON_INFORMATION)
        dialogo.ShowModal()
        self.codigo_cuadro.Clear()
        self.producto_cuadro.Clear()
        self.costo_cuadro.Clear()
        dialogo.Destroy()



    def OnBuscar(self, evt):
        from prueba import MyFrame2




    def OnSalir(self, evt):
        self.Parent.Close()




class App(wx.App):
    def OnInit(self):
        f = MyFrame(parent = None, title = u'Inventario', size = (200,400), pos = (320,150))
        f.Show()
        return True

aplicacion = App(0)
aplicacion.MainLoop()

call to prueba.py

import wx
import sqlite3

conn = sqlite3.connect('formulario.dat')
cur = conn.cursor()

cur.execute("""SELECT * FROM inventario""")
list = list(cur.fetchall())
index = range(len(list))

class App(wx.App):
    def OnInit(self):
        self.ventana = wx.Frame(parent = None, title = u'Resultados.', size = (200,400), pos = (320,150))
        panel = wx.Panel(self.ventana, -1)
        codigo = wx.StaticText(panel, -1, 'Codigo', pos = (20,30))
        self.codigocuadro = wx.TextCtrl(panel, -1, '', pos = (20, 50))
        self.codigocuadro.Bind(wx.EVT_TEXT, self.Buscar)

        return True

    def Buscar(self, evt):

        cur.execute("""SELECT * FROM inventario""")
        all = cur.fetchall()
        criterio = self.codigocuadro.GetValue()
        if criterio <> '':
            cur.execute("""SELECT * FROM inventario WHERE codigo_cuadro LIKE ('%%%s%%')""" %(criterio))
            items = cur.fetchall()
        else:
            items = []



aplicacion = App()
aplicacion.MainLoop()

Thanks .. I hope I help please

Edited 3 Years Ago by mike_2000_17: Fixed formatting

SQLite is separate from wxPython, so the two are independent of each other. You can enter a value in wxPython, retrieve the value from the wxPython entry box, and then add it to a SQLite database, or vice-versa. I would suggest starting with a simple program that asks for a name, for example, and adds the name to a database. It doesn't matter what it is as long as it is simple . Use one class only for all of the wx statements, and another class for the SQLite statements. Next you can add another function to ask for a name to look up in the database and display the results. You have posted 99 lines of code, and have not stated clearly what the problem is. Start with a smaller program that makes it easy to understand what it does, and state any problems, i.e. what was expected and what actually happened. For example, the wx class would instantiate the SQLite class, which would open the file when the __init__ function is called. You would then retrieve the name or whatever, still in the wx class, and pass it to an "add" function in the SQLite class which would add the record, etc.

Edited 5 Years Ago by woooee: n/a

thanks for your response sir, could you tell me how to do to query data in a database please?

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