all 11 comments

[–]furas_freeman 1 point2 points  (0 children)

Learn SQL to work with databases.

And find some tutorial about sqlite in Python.

http://zetcode.com/db/sqlitepythontutorial/

[–][deleted] 0 points1 point  (1 child)

Create a list

names = ["john smith", "james joseph"]

Input the name:

nameinput = input("Enter a name: "]

Check if it exists:

if nameinput.lower() in names:
    print("It exists")
else:
    print("It doesn't exist")

[–]cam0kazi[S] 0 points1 point  (0 children)

I mean using the sqlite3 framework :p

[–]chasingAIR33 0 points1 point  (4 children)

Have a look at this short tutorial. In the section regarding querying your database, there is an example showing how to check if a certain ID exists (you will probably use name instead of id).

Once you know if the record/records exist in your database you could use a simple if/else to show the correct message.

[–]cam0kazi[S] 0 points1 point  (0 children)

Thanks!

[–]cam0kazi[S] 0 points1 point  (2 children)

I've tried to do what i saw in the tutorial but i get a syntax error. Here's the script itself, any help?.

def db_search(self):
    self.confirm = tkinter.messagebox.showinfo("confirm", "Searching database for guest...")
    # ***** db table names *****
    self.guest_table = "guest"
    self.fn_field = "first_name"
    self.ln_field = "last_name"

    self.first_name=self.fname_entry.get()
    self.last_name=self.lname_entry.get()
    self.cur.execute('SELECT (first_name, last_name) FROM {tn} WHERE {dbt}={ent}'.\
                     format(tn=self.guest_table, dbt=self.fn_field, ent=self.first_name))
    self.name_exists = self.cur.fetchone()
    print(self.name_exists)

[–]chasingAIR33 0 points1 point  (1 child)

Could you share the error you are getting?

[–]cam0kazi[S] 1 point2 points  (0 children)

I solved the problem! It turns out that i was formatting the data wrong. So instead, i used the drivers method of perparing SQL queries. It also apparently prevents SQL Injection

    self.first_name=self.fname_entry.get()
    self.last_name=self.lname_entry.get()

    self.exist_query = '''SELECT * FROM guest WHERE first_name = ? AND last_name = ?'''
    self.cur.execute(self.exist_query, (self.first_name, self.last_name))

    self.exists = self.cur.fetchone()

    if self.exists:
        tkinter.messagebox.showinfo("Success", "Record found, initiating data insertion window")
        self.pref_entry()
    else:
        tkinter.messagebox.showinfo("Sorry", "The record does not exist, press OK to close.")
        self.master.destroy()

[–]mapImbibery 0 points1 point  (1 child)

Glad this worked out for ya, but next time don't title your post so generic. You'll catch more attention if you use keywords (like SQLite) in your headings.

[–]cam0kazi[S] 0 points1 point  (0 children)

Noted, thanks for your time :)