Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using if statement with database in python

veri = sqlite3.connect('IBCKayıt')
v = veri.cursor()

v.execute("SELECT numsıra FROM kayıtlar WHERE EXISTS(SELECT * FROM kayıtlar WHERE numsıra)")
record = v.fetchall()

if numaraentry.get() in record:
    messagebox.showinfo("UYARI","Bu IBC Zaten Var")

if numaraentry.get() not in record:
    v.execute("INSERT INTO kayıtlar VALUES (:numsıra, :koruyucutarih, :numunedurumm, :kullandurumm)",
        {'numsıra':numaraentry.get(),
         'koruyucutarih':tarıhgırıs,
         'numunedurumm':secılmıs1.get(),
         'kullandurumm':secılmıs.get()
        })
    messagebox.showinfo("UYARI","Kayıt İşlemi Başarılı.")


veri.commit()
veri.close()

I want it to give a warning when the same data is entered, but when I enter the same data, it does not give a warning. What do you think is the solution?

-I’m new to the form, I guess I don’t know how to open a topic, sorry.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your first query doesn’t make much sense; you’ll want to use WHERE there.

Something like

veri = sqlite3.connect("IBCKayıt")

num = numaraentry.num()

v = veri.cursor()

v.execute("SELECT numsıra FROM kayıtlar WHERE numsıra = ? LIMIT 1", (num,))
records = v.fetchall()

if records:  # had a matching row, show error
    messagebox.showinfo("UYARI", "Bu IBC Zaten Var")
else:
    v.execute(
        "INSERT INTO kayıtlar VALUES (:numsıra, :koruyucutarih, :numunedurumm, :kullandurumm)",
        {
            "numsıra": num,
            "koruyucutarih": tarıhgırıs,
            "numunedurumm": secılmıs1.num(),
            "kullandurumm": secılmıs.num(),
        },
    )
    veri.commit()
    messagebox.showinfo("UYARI", "Kayıt İşlemi Başarılı.")

veri.close()

should do the trick.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading