通过在输入框中输入标识号并按下按钮来执行 Oracle SQL 查询

Execute Oracle SQL query by input Identification number inside entry box and pressing button

我是 python 的新手。我尝试开发一个程序,它在数据库中执行一些 Oracle SQL 查询。所以接下来的概念是:我在这里有一个输入框,我输入一个个人 ID,然后我必须按下从输入框中收集输入的个人 ID 并将其注入到已经准备好的 SQL 查询中的按钮,然后查询将被执行和 select 有关个人 ID 所有者的数据。现在我卡住了,我无法从输入框中收集数据。请帮忙。非常感谢。

import tkinter as ibumt
from tkinter import simpledialog
import cx_Oracle

def get_pn():
    global pn
    result = simpledialog.askstring
    pn = result # pn is like 01000476891

sql_pn = """ Select cl.EXTERNALID as CLIENT_NO , upc.USERNAME, ipe.FIRSTNAMEINLATIN as FIRSTNAME, ipe.LASTNAMEINLATIN as LASTNAME, ipe.IDENTIFICATIONNUMBER
where 1=1 and cpc3con.CONTRACTSUBTYPE = 'STB'
and dcu.STATUS='ACTIVE'
and ipe.IDENTIFICATIONNUMBER = :pn and rownum = 1 """""
#ipe.IDENTIFICATIONNUMBER = :pn << pn must automatically replaced by inputted pn number for example: 01000476891

con = cx_Oracle.connect('Test/test@160.28.76.89:1521/STBDB')
cur = con.cursor()
cur.execute(sql_pn, [pn])
for line in cur:
    print(line)
cur.close()
con.close()

width = 350
heigh = 600

root = ibumt.Tk()
root.title('ibumt')
root.resizable(height=False, width=False)

canvas = ibumt.Canvas(root, height=heigh, width=width)
canvas.pack()

frame_hider = ibumt.Frame(root, bg='gray')

frame_hider.place(relx=0.5, rely=0.005, relheight=0.07,relwidth=0.99, anchor='n')

label_hider = ibumt.Label(frame_hider, text="User Management Tool",   fg='black', bg='gray')
label_hider.config(font=("Courier", 9))
label_hider.place(relx=0.15, rely=0.25, relheight=0.5, relwidth=0.7)

frame_mid = ibumt.Frame(root, bg='dark gray')
frame_mid.place(relx=0.005, rely=0.08, relheight=0.6, relwidth=0.99)

label_pn = ibumt.Label(frame_mid, text="Personal Number :",   fg='white', bg='dark gray')
label_pn.place(relx=0.01, rely=0.02, relheight=0.08, relwidth=0.34)

entry_pn = ibumt.Entry(frame_mid, bg='white')
entry_pn.place(relx=0.37, rely=0.02, relheight=0.08, relwidth=0.35)

button_pn = ibumt.Button(frame_mid, text="Check Client", bg='gray',command=get_pn)
button_pn.place(relx=0.74, rely=0.02, relheight=0.08, relwidth=0.25)

label_output_pn_cn = ibumt.Label(frame_mid, fg='white', bg='white')
label_output_pn_cn.place(relx=0.009, rely=0.225, relheight=0.76,relwidth=0.983)

root.mainloop()

您必须使用 StringVar() 对象。

import tkinter as ibumt
from tkinter import simpledialog
import cx_Oracle

def get_pn():
    global pn
    result = my_stringvar.get() #this will set result to whatever is in the entry box at the time
    pn = result # pn is like 01000476891

sql_pn = """ Select cl.EXTERNALID as CLIENT_NO , upc.USERNAME, ipe.FIRSTNAMEINLATIN as FIRSTNAME, ipe.LASTNAMEINLATIN as LASTNAME, ipe.IDENTIFICATIONNUMBER
where 1=1 and cpc3con.CONTRACTSUBTYPE = 'STB'
and dcu.STATUS='ACTIVE'
and ipe.IDENTIFICATIONNUMBER = :pn and rownum = 1 """""
#ipe.IDENTIFICATIONNUMBER = :pn << pn must automatically replaced by inputted pn number for example: 01000476891

con = cx_Oracle.connect('Test/test@160.28.76.89:1521/STBDB')
cur = con.cursor()
cur.execute(sql_pn, [pn])
for line in cur:
    print(line)
cur.close()
con.close()

width = 350
heigh = 600

root = ibumt.Tk()
root.title('ibumt')
root.resizable(height=False, width=False)

my_stringvar = ibumt.StringVar() #create an instance of StringVar()

canvas = ibumt.Canvas(root, height=heigh, width=width)
canvas.pack()

frame_hider = ibumt.Frame(root, bg='gray')

frame_hider.place(relx=0.5, rely=0.005, relheight=0.07,relwidth=0.99, anchor='n')

label_hider = ibumt.Label(frame_hider, text="User Management Tool",   fg='black', bg='gray')
label_hider.config(font=("Courier", 9))
label_hider.place(relx=0.15, rely=0.25, relheight=0.5, relwidth=0.7)

frame_mid = ibumt.Frame(root, bg='dark gray')
frame_mid.place(relx=0.005, rely=0.08, relheight=0.6, relwidth=0.99)

label_pn = ibumt.Label(frame_mid, text="Personal Number :",   fg='white', bg='dark gray')
label_pn.place(relx=0.01, rely=0.02, relheight=0.08, relwidth=0.34)

entry_pn = ibumt.Entry(frame_mid, bg='white', textvariable=my_stringvar)# you have to set the textvariable to your StringVar() instance.
entry_pn.place(relx=0.37, rely=0.02, relheight=0.08, relwidth=0.35) 

button_pn = ibumt.Button(frame_mid, text="Check Client", bg='gray',command=get_pn)
button_pn.place(relx=0.74, rely=0.02, relheight=0.08, relwidth=0.25)

label_output_pn_cn = ibumt.Label(frame_mid, fg='white', bg='white')
label_output_pn_cn.place(relx=0.009, rely=0.225, relheight=0.76,relwidth=0.983)

root.mainloop()