2

am have created a table using treeview and i would like to insert in data fetched from mysql table.if any one can help me because i have tried all my level best but still in vain.with this statement tree.insert("", 1, text=2, values=("name", "5", "5")) can insert well data but not from the database, but i would like to fetch from the database and display it. here is the code i have tried but it has failed.please help. `

from Tkinter import *
import ttk
import MySQLdb

root = Tk()
root.geometry("320x240")

tree = ttk.Treeview(root)

conn = MySQLdb.connect("localhost", "root", "drake", "OSCAR")
cursor = conn.cursor()

tree["columns"] = ("one", "two", "three")
tree.column("one", width=100)
tree.column("two", width=100)
tree.column("three", width=100)

tree.heading("#0", text='ID', anchor='w')
tree.column("#0", anchor="w")
tree.heading("one", text="NAME")
tree.heading("two", text="VOTES")
tree.heading("three", text="PERSENTAGE")

for i in range(1, 6):
    cursor.execute("""select name from president where ID =%s""", (i,))
    nm = cursor.fetchone()[0]
    cursor.execute("""select votes from president where ID =%s""", (i,))
    vot = cursor.fetchone()[0]
    cursor.execute("""select percentage from president where ID =%s""",(i,))
    percent = cursor.fetchone()[0]

    tree.insert("", i, text=i, values=(nm, vot, percent)),

tree.pack()
root.mainloop()

`

2 Answers 2

2

To resolve your problem, first you will need to read all the rows of the database using this query:

SELECT * FROM president

which you need to execute:

cursor.execute("""SELECT * FROM president""")

Now, simply loop over the rows and insert them one by one in tree:

UPDATE:

I suppose your table structure is like this:

ID | name | votes | percentage

So you could run this:

cpt = 0 # Counter representing the ID of your code.
for row in cursor:
   # I suppose the first column of your table is ID
   tree.insert('', 'end', text=str(cpt), values=(row[1], row[2], row[3]))
   cpt += 1 # increment the ID
Sign up to request clarification or add additional context in comments.

5 Comments

hello@Bill,am receiving an Typeerror: 'cursor' object is not callable.its from the for (name,votes,percentage) in cursor:
Check my edit. Tell me if the structure of your table is as I mentioned @ByamukamaOscar
yes,its like that and am going to try out and see .thanks for effort you have put @bill
If you have further troubles with this code, do not hesitate to ask
Why ttk.Scrollbar not working when fetching data from database. It is working while inserting data directly to tree
1

iv used sqlite3 not MySQL but i am assuming that the value that is returned from the sql is put into a multidimensional array, these are array which require more than one index e.g

array[0][1]

the code below is for modifying the tree

for i in self.tree.get_children():
    self.tree.delete(i) #clears current values from tree

for student in StudentList:
    self.tree.insert("" , 0,values=(student[0],student[1])
    #the index used would depend on what you want to be put into the tree
    #only uses one index per value instead of two as the for loop changes the first index

note that this was copied from my coursework(booking system) hence the names used

Comments

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.