2

Am trying to display content entered into entry widget in treeview after saving it in sqlite3 db.The content saved into db but doesn't display the id,Fist name and Surname contents in the treeview.

Your suggestion are welcome to achieve this.

from tkinter import ttk
import tkinter as tk
import sqlite3


def connect():
    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, 
First TEXT, Surname TEXT)")
    conn.commit()
    conn.close()



def Insert():
    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    fr=cur.execute("INSERT INTO profile (First, Surname) VALUES(?, ?)", 
    (first_text.get(), surname_text.get()))
    conn.commit()
    for items in fr:
        tree.insert('', tk.END, values=items)

    conn.close()



connect()  #  this to create the db


root = tk.Tk()
root.geometry("400x400")

tree= ttk.Treeview(root, column=("column", "colunn1"))
tree.heading("#0", text="NUMBER")
tree.heading("#1", text="FIRST NAME")
tree.heading("#2", text="SURNAME")
tree.pack()


first_text = tk.StringVar()
e1 = tk.Entry(root, textvariable=first_text)
e1.pack()
surname_text = tk.StringVar()
e2 = tk.Entry(root, textvariable=surname_text)
e2.pack()

b1 = tk.Button(text="add data", command=Insert)
b1.pack(side=tk.BOTTOM)

root.mainloop()
O JOE
  • 587
  • 2
  • 17
  • 31

1 Answers1

2

The problem in your code is that for items in fr does not work. Python sees fr as an empty iterable (put a print statement in the for loop and you will see that it is never executed). So, to insert the data in the treeview, you can get it directly from the entries and retrieve the db id with cur.lastrowid (I have found this solution in the answer to the question How to retrieve inserted id after inserting row in SQLite using Python?):

from tkinter import ttk
import tkinter as tk
import sqlite3


def connect():
    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, First TEXT, Surname TEXT)")
    conn.commit()
    conn.close()



def Insert():
    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    data = (first_text.get(), surname_text.get())
    # insert data in db
    cur.execute("INSERT INTO profile (First, Surname) VALUES(?, ?)", data)  
    conn.commit()
    # insert data in treeview
    tree.insert('', tk.END, text=str(cur.lastrowid), values=data)  
    conn.close()


connect()  #  this to create the db

root = tk.Tk()
root.geometry("400x400")

tree= ttk.Treeview(root, column=("column", "colunn1"))
tree.heading("#0", text="NUMBER")
tree.heading("#1", text="FIRST NAME")
tree.heading("#2", text="SURNAME")
tree.pack()


first_text = tk.StringVar()
e1 = tk.Entry(root, textvariable=first_text)
e1.pack()
surname_text = tk.StringVar()
e2 = tk.Entry(root, textvariable=surname_text)
e2.pack()

b1 = tk.Button(text="add data", command=Insert)
b1.pack(side=tk.BOTTOM)

root.mainloop()

EDIT: If you use the columns '#1', '#2', '#3' to avoid using the special column '#0', then you need to change a bit Insert: you need to pass the row id in values instead of text.

from tkinter import ttk
import tkinter as tk
import sqlite3

def connect():
    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, First TEXT, Surname TEXT)")
    conn.commit()
    conn.close()


def Insert():
    conn = sqlite3.connect("TRIAL.db")
    cur = conn.cursor()
    data = (first_text.get(), surname_text.get())
    # insert data in db
    cur.execute("INSERT INTO profile (First, Surname) VALUES(?, ?)", data)  
    conn.commit()
    # insert data in treeview
    tree.insert('', tk.END, values=(str(cur.lastrowid),) + data)  
    conn.close()


connect()  #  this to create the db

root = tk.Tk()
root.geometry("400x400")

tree = ttk.Treeview(root, column=("column1", "column2", "column3"), show='headings')
tree.heading("#1", text="NUMBER")
tree.heading("#2", text="FIRST NAME")
tree.heading("#3", text="SURNAME")
tree.pack()

first_text = tk.StringVar()
e1 = tk.Entry(root, textvariable=first_text)
e1.pack()
surname_text = tk.StringVar()
e2 = tk.Entry(root, textvariable=surname_text)
e2.pack()

b1 = tk.Button(text="add data", command=Insert)
b1.pack(side=tk.BOTTOM)

root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • when i hide the first column with this e.g(tree= ttk.Treeview(root, column=("column", "colunn1", "columns2"), show="headings")) the row id doesn't display under the but the first entry appears under the `NUMBER` and the second entry under `First name` leaving the Surname empty. when i insert data into it. – O JOE Feb 14 '18 at 12:12
  • @OJOE Are you using columns '#0', '#1', '#2' or '#1', '#2', '#3'? Because I think than in a later question, the solution was to switch from '#0', '#1', '#2' to '#1', '#2', '#3' and that might be why this answer no longer works with your current code. – j_4321 Feb 14 '18 at 12:18
  • Yeaa I changed it to " #0" – O JOE Feb 14 '18 at 12:35
  • I changed it to ' #1',' #0','#2' as I results of answer you provided in one my question then I found out that row wasn't appearing under it column. – O JOE Feb 14 '18 at 12:38