0

i am trying to output 2 different returns from sql queries in to the respective columns in a treeview widget using tkinter module in python 3.4 When i run command defined below the first column prints all the entries correctly but the name column prints the name of the first result in all rows instead of name per respective row. Any ideas on what im doing wrong?

    def refreshtrade():

        for i in treeview.get_children():
            treeview.delete(i)


            #order number
        refreshtradein = conn.cursor()                
        refreshtradein.execute("SELECT increment_id FROM mg_ikantam_buyback_order")

            #first name
        names =conn.cursor()
        names.execute("SELECT customer_firstname FROM mg_ikantam_buyback_order")# WHERE increment_id = 'buyback-%s'" %(tradeinentryfield.get() ))

        for n in names:
            for r in refreshtradein:
                     treeview.insert('',0,r,text = r, values=(n,'Mercedes', 'Purchased', '8-34-15'))



        refreshtradein.close()
        conn.close()
Ray Mercedes
  • 35
  • 1
  • 9

1 Answers1

1

Why are you using two different cursors and consequently two nested for loops? Are you aware how nested for loops are evaluated?

querycursor = conn.cursor()
querycursor.execute(SELECT increment_id, customer_firstname FROM mg_ikantam_buyback_order)

for row in querycursor:
    print(row[0])
    print(row[1])

Oh and regarding your where clause. Don't ever do parameter substitution like that. It is great security risk

See here how to do it correctly

Community
  • 1
  • 1
Christian Rapp
  • 1,853
  • 24
  • 37
  • thanks for the feedback and no im not aware so thanks for the insight. i will try to make sense of your response as i am very new to coding and it is not intuitive to me yet but i will respond with my results. thanks again – Ray Mercedes Aug 28 '15 at 21:31
  • i get a AttributeError: 'tuple' object has no attribute 'increment_id' error when i try your suggestion – Ray Mercedes Aug 28 '15 at 22:00
  • thanks man, im still very new to this obviously and i appreciate you help. that made it output exactly what i needed. i tried the non security risk sql query exploit but i get a syntax error when i put the comma :/ – Ray Mercedes Aug 29 '15 at 08:12