Python:如何将从文本框中获取的参数传递给PYODBC查询

时间:2017-01-20 16:26:43

标签: python sql tkinter

我无法将从文本框输入获取的参数传递到SQL查询以解锁用户帐户。

我在查询中使用显式的显式值测试代码,并且它成功运行。

我感谢任何帮助!!

from Tkinter import *
import pypyodbc as pyodbc
master = Tk()
username = StringVar()
def callback():
    print e.get()

def unlockhrs():
    username = e1.get()
    print(username)
    cnxn = pyodbc.connect('Trusted_Connection=yes;DRIVER={SQL Server};SERVER=xxx;DATABASE=xxx;UID=domain1\xxx;PWD=xxx')
    cursor = cnxn.cursor()
    cursor.execute("update tbl_UserActivity set UserBlock = 0 where UserName =" +username)
    cursor.commit()
    cursor.execute("select * from tbl_UserActivity where UserName =" +username)


#master = Tk()

master.title("HRS UNLOCK")
master.resizable(False, False)
master.minsize(width=300, height=50)
master.maxsize(width=300, height=50)
Label(master, text="USERNAME").grid(row=0)
e1 = Entry(master)
e1.grid(row=0, column=1)
e1.bind('<Return>', unlockhrs)





Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Unlock', command=unlockhrs).grid(row=3, column=2, sticky=W, pady=4)

mainloop( )

1 个答案:

答案 0 :(得分:1)

经过一些麻烦和布莱恩的暗示:

...
def unlockhrs():
    username = e1.get()
    print(username)
....
e1 = Entry(master)
e1.grid(row=0, column=1)
e1.bind('<Return>', unlockhrs)
...