Python Forum
Error Handling/No results from SQL Query
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error Handling/No results from SQL Query
#1
Hello - I have what I would think should be a fairly simple problem, but I just cannot get it to work. The app I'm building is pretty straightforward. User opens the app, enters a value (a problem ID, called PROBLEMID) and clicks a button. The app checks a SQL table for that value and returns back a user name.

I have gotten it to work when the problem ID is FOUND in the database table, but when the id doesn't exist, I just get an error. Can you help set me straight? I can't get TRY to work, or any IF not or IF !=

Thank you in advance for any help.

Here's the entirety of my code (and yes, I know it's lousy and inefficient)
import tkinter as tk #python3.x
from tkinter import messagebox
#import Tkinter as tk #python2.x
from tkinter import *
import pyodbc

#Set Up App Window & defaults
root = tk.Tk()
root.option_add("*Font", "Verdana")
root.configure(background='black')
root.geometry('220x125') # Size WxH
root.title("SERV OWNER FINDER")

#create labels and entry boxes
PROBIDLABEL = Label(root, text="Enter Problem ID ", bg = "Black", fg = "gray")
PROBIDENTRY = Entry(root, bg="light yellow")

PROBIDLABEL.place(x=1,y=3)
PROBIDENTRY.place(x=1,y=30)

def close_window(): 
    root.destroy()

def checktable():
        problemid = "'" + PROBIDENTRY.get() + "'"
        connstring = 'DRIVER={SQL Server};SERVER=OBDEVSQLAGL05;DATABASE=DCollection;UID=xxxx;PWD=yyyy'
        SQLstr="select OWNER from GD_SERV_ADMIN where PROBLEMID = " + problemid
        conn=pyodbc.connect(connstring)
        cursor=conn.cursor()
        cursor.execute(SQLstr)
        row = cursor.fetchone()
        messagebox.showinfo("Owner found", "The ADMIN for this problem ID is:  " + row[0])
        cursor.close()
        PROBIDENTRY.delete(0,END)
        
#Create Buttons
SubmitButton = Button(root, text="Submit", width = 8, command=checktable)
QuitButton = Button(root, text="Quit",width = 8, command=close_window)

#Place Buttons on Window
QuitButton.place(x=5, y=70)
SubmitButton.place(x=120, y=70)

root.mainloop()
Reply


Messages In This Thread
Error Handling/No results from SQL Query - by JP_ROMANO - Jul-17-2018, 08:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,282 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Help needed with a "for loop" + error handling tamiri 2 2,528 May-27-2022, 12:21 PM
Last Post: tamiri
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,114 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  pymysql: insert query throws error wardancer84 12 4,596 Jan-28-2022, 06:48 AM
Last Post: wardancer84
  Query Syntax Error hammer 2 1,628 Jan-03-2022, 02:30 PM
Last Post: hammer
  Handling Python Fatal Error richajain1785 7 5,917 Oct-14-2021, 01:34 PM
Last Post: Tails86
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 5,129 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  Error using mariadb select query with form in python? shams 2 2,021 Jul-29-2021, 12:30 PM
Last Post: shams
  Error Handling JarredAwesome 5 2,939 Oct-17-2020, 12:41 AM
Last Post: JarredAwesome
  Compiling Python 3.8.5 source code results in build error Deepan 0 2,192 Sep-14-2020, 04:11 AM
Last Post: Deepan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020