2

I am pulling a variable from the user when they select one of the radio buttons I provide them. Each radio button variable is set as an actual server I can normally connect to using MsSql.

My below code has a !!!!!!!!!!!!! MY EXAMPLE !!!!!!!!!!!!!!!!!! listed next to the spot I am trying to figure out.

I get a connection error when I use pypyodbc.connect ('Server=serverName;'), but I know it works if I get rid of the radio button variable and enter the actual server name.

Any ideas?

#! /usr/bin/python
import os
import pypyodbc
import tkinter
from tkinter import ttk


class Adder(ttk.Frame):
    """The adders gui and functions."""
    def __init__(self, parent, *args, **kwargs):
        ttk.Frame.__init__(self, parent, *args, **kwargs)
        self.root = parent
        self.init_gui()

    def on_quit(self):
        """Exits program."""
        quit()

    def calculate(self):
        serverName = str(self.selectedTown.get())  #! !!!!!!!!!!!!! MY EXAMPLE  !!!!!!!!!!!!!!!!!!
        word1 = str(self.workstation1_entry.get())
        # num2 = int(self.localid2_entry.get())  # This will be entered in a later statement where user can enter LocationID
        # if str(self.selectedTown.get()) in str(self.selectedTown.get()):
        connection = pypyodbc.connect('Driver={SQL Server};'  #! !!!!!!!!!!!!! MY EXAMPLE  !!!!!!!!!!!!!!!!!!
                                            'Server=serverName;'
                                            'Database=mydatabase;'
                                            'Trusted_Connection=yes;')
        cursor = connection.cursor() 
        SQLCommand = ("SELECT Name, Location_ID "      
            "FROM dbo.PB_Location "
            "WHERE Name = ?")
        Values = [word1]
        cursor.execute(SQLCommand,Values)
        results = cursor.fetchone()
        if results:
            self.answer_label['text'] = str(results[1]) # displays answer
            connection.close()
        else:
            self.answer_label['text'] = "Invalid"
        connection.close()



    def init_gui(self):
        """Builds GUI."""
        self.root.title('ID Lookup')
        self.root.option_add('*tearOff', 'FALSE')

        self.grid(column=0, row=0, sticky='nsew')

        self.menubar = tkinter.Menu(self.root)

        self.menu_file = tkinter.Menu(self.menubar)
        self.menu_file.add_command(label='Exit', command=self.on_quit)

        self.menu_edit = tkinter.Menu(self.menubar)

        self.menubar.add_cascade(menu=self.menu_file, label='File')
        self.menubar.add_cascade(menu=self.menu_edit, label='Edit')

        self.root.config(menu=self.menubar)

        self.workstation1_entry = ttk.Entry(self, width=5)
        self.workstation1_entry.grid(column=1, row = 2)

        self.localid2_entry = ttk.Entry(self, width=5)
        self.localid2_entry.grid(column=3, row=2)

        self.calc_button = ttk.Button(self, text='Submit',
                command=self.calculate)
        self.calc_button.grid(column=0, row=3, columnspan=4)

        self.answer_frame = ttk.LabelFrame(self, text='Answer',
                height=100)
        self.answer_frame.grid(column=0, row=4, columnspan=4, sticky='nesw')

        self.answer_label = ttk.Label(self.answer_frame, text='')
        self.answer_label.grid(column=0, row=0)


        self.selectedTown = tkinter.StringVar()
        self.selectedTown.set('b1') # make it where the top radio button is selected by default

        self.b1 = ttk.Radiobutton(self, text='Texas', value='1srvodbx', variable=self.selectedTown).grid(sticky='W', column=0,row=6, columnspan=1)  # sticky W to align everything to left          
        self.b2 = ttk.Radiobutton(self, text='Oklahoma', value='2srvodbx', variable=self.selectedTown).grid(sticky='W', column=0,row=7, columnspan=1)
        self.b3 = ttk.Radiobutton(self, text='Idaho', value='3srvodbx', variable=self.selectedTown).grid(sticky='W', column=0,row=8, columnspan=1)

        self.selectedTown.get()

        # Labels that remain constant throughout execution.
        ttk.Label(self, text='Location ID Finder').grid(column=0, row=0,
                columnspan=4)
        ttk.Label(self, text='Name').grid(column=0, row=2,
                sticky='w')
        ttk.Label(self, text='Location ID').grid(column=2, row=2,
                sticky='w')

        ttk.Separator(self, orient='horizontal').grid(column=0,
                row=1, columnspan=4, sticky='ew')

        for child in self.winfo_children():
            child.grid_configure(padx=5, pady=5)

if __name__ == '__main__':
    root = tkinter.Tk()
    Adder(root)
    root.mainloop()
nbro
  • 15,395
  • 32
  • 113
  • 196
Prox
  • 699
  • 5
  • 11
  • 33
  • I tried that and no luck. Is there a way I can get python to show me what it's entering as a variable? The error I am getting from python and pypyodbc is... DatabaseError: ('08001', '[08001] [Microsoft] [ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied.') I know it's not entering the exact value name because I can get it to provide be sql data when I enter the server name in myself. – Prox Mar 16 '17 at 23:13
  • For example, 1srvodbx is a server name. I'm really new to Tkinter. I'm wondering if I'm setting my variable up correctly. Time to read some more on it. – Prox Mar 16 '17 at 23:20
  • Have you done `print serverName` or `print(serverName)` if you are on 3.x? – mechanical_meat Mar 16 '17 at 23:23
  • It actually prints the servername as it should look. For some reason pypyodbc.connect is not reading. – Prox Mar 16 '17 at 23:47
  • I thought it worked by 'Server=(serverName);' but I was wrong. – Prox Mar 16 '17 at 23:55
  • Please see my answer. I think I know what you're doing wrong... – mechanical_meat Mar 17 '17 at 00:00

1 Answers1

0

IIUC the problem is you cannot just plug in a variable name like that. You'll have to do:

connstr = '''Driver={{SQL Server}};Server={};Database=mydatabase;Trusted_Connection=yes;'''.format(serverName)
connection = pypyodbc.connect(connstr)
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • Do you mean I need to actually write a format type in place of the word .format ? – Prox Mar 17 '17 at 00:23
  • no, its just one word. When I do that line verbatim, it gives me: KeyError: 'SQL Server' Also, the ''' ''' is commenting out the line. – Prox Mar 17 '17 at 00:28
  • Do you happen to know of a guide that explains that? I want to read up on it. – Prox Mar 17 '17 at 00:39