0

i have a python code that creates a gui where i make the query using a cursor (=curs) asking from the user to enter a serial number. According to this number the oracle table returns a result (ie 1111111). After that, I want a second query from another table: ‘select project_name from customer_desc where customer_name_d= the value in the customer box(=1111111)'. Any ideas? I need to make a new connection to the base? How can I set the where clause in order to read the textvalue returned to gui text field, with blind variables? Thanks

   import cx_Oracle
   from tkinter import*
   from tkinter import messagebox

   def search():
   try:

    connstr='SOLVATIO/SOLVATIO@localhost'
    conn = cx_Oracle.connect(connstr)
    curs = conn.cursor()
    curs.execute("select * from customers where afm='%s'"%afm.get())
    result=curs.fetchone()
    company_name.set(result[1])
    e1.configure(state='disabled')
    conn.close()

    def clear():
   afm.set('')
   company_name.set('')
   e1.configure(state='normal')

   a1=Tk()
   a1.title('SOLVATIO')
   a1.geometry('600x300')
   ptitle=Label(a1, text='''search asset''')
   ptitle.grid(row=0, column=0, columnspan=2)

   afm=StringVar()
   company_name=StringVar()
   l1=Label (a1, text=' AFM ')
   e1=Entry(a1, textvariable=afm)
   l2=Label (a1, text=' customer ')
   e2=Entry(a1, textvariable=company_name)
   b1=Button(a1, text=' Search ', command=search)
   l1.grid(row=1, column=0)
   e1.grid(row=1, column=1)
   l2.grid(row=2, column=0)
   e2.grid(row=2, column=1)
   b1.grid(row=1, column=2)
   a1.mainloop()
1
  • Your question doesn't have enough detail about how you are invoking/running Python to answer (unless someone can guess what %afm is). On a more important topic you MUST use bind variables instead of the %s string build up, otherwise you have a big security hole and will have poor scalability. Check out some of the cx_Oracle samples or follow the tutorial: github.com/oracle/python-cx_Oracle/tree/master/samples/tutorial Commented Jun 9, 2018 at 1:20

1 Answer 1

1

You're most of the way there. To use bind variables you need to do the following:

curs.execute("select * from customers where afm=:1", [afm.get()])

The rest of your code worked as is.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, i tried it and worked. For the second part of my question, how to put inside my second query the value of the first? i mean, there is any link or suggestions what is the syntax of reading from a gui textbox?
What second query? The example I gave with customers is reading from a GUI textbox and using the contents as a bind variable...
Sorry, I misunderstood your answer because of the ‘afm’ column that I use in my first select. It worked, thanks a lot!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.