OK I have a search script that suppose to search for information in my database. The problem I having is that I want the user to be able to enter a last name to search.

The only part that is giving me problems is this part, I'm stuck on this part.

db.execute("SElECT fname, lname, phone, address, email from contacts WHERE lname = (results)")

Traceback (most recent call last):
File "C:/Users/Panic/Desktop/Contacts/search.py", line 46, in OnStart
db.execute("SElECT fname, lname, phone, address, email from contacts WHERE lname = (results)")
sqlite3.OperationalError: no such column: results


Here is the rest of the code that I am not having problems with.

#Importing modules

import os
import wx
import sqlite3
import Database

#Creating a class

class search(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Search', size = (400,100))
        panel = wx.Panel(self, -1)

#Centering the frame

        self.Centre()

#Creating input fields

        searchLabel = wx.StaticText(panel, -1, "          Search:")
        self.search = wx.TextCtrl(panel, -1, "Enter last name")
        searchLabel.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL))
        self.search.SetInsertionPoint(0)

#Creating sizers

        sizer = wx.FlexGridSizer(cols = 8, hgap = 8, vgap = 8)
        sizer.AddMany([searchLabel, self.search])
        panel.SetSizer(sizer)

#Creating buttons and events

        start = wx.Button(panel, label = 'Start', pos = (225, 0), size = (80, 25))
        start.Bind(wx.EVT_BUTTON, self.OnStart, start)

        results = self.search.GetValue()

    def OnStart(self, event):
        dlg = wx.MessageBox("Start search?", 'Message', wx.YES_NO | wx.ICON_QUESTION)

        if (dlg == wx.YES):

            file = open (r'Database.py', 'r')
            exec(file.read())
            db.execute("SElECT fname, lname, phone, address, email from contacts WHERE lname = (results)")
            

        else:
            self.Destroy()


if __name__ == '__main__':
    myApp = wx.App()
    frame = search()
    frame.Show()
    myApp.MainLoop()

Thanks to anyone who can lend help with this.

Thanks to anyone who can lend help with this.

You've simply forgotten to put the value of results into the string. Instead you mistakenly typed the name results. Here's how you should use string formatting to add the value:

db.execute("SElECT fname, lname, phone, address, email from contacts WHERE lname = (%s)" % results)

HTH

I believe there may be two problems. Results appears to be undefined within the function. Either pass in results as an argument or use self.results. (The original definition of results should also be made in the method above.

Secondly, the syntax of the pysqlite statement appears to be incorrect try..

db.execute("SElECT fname, lname, phone, address, email from contacts WHERE lname = %s" % self.results)
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.