0
def query():
    global record
    conn = sqlite3.connect('billing.db')
    c = conn.cursor()
    c.execute("SELECT *, oid FROM billing")
    records = c.fetchall()
    # print(records)

    print_records = ''
    for record in records:
        print_records += str(record[0]) + "    " + str(record[1]) + "         " + str(record[2]) \
                         + "     " + str(record[3]) + "     " + str(record[4]) + "     " + str(record[5]) \
                         + "     " + str(record[6]) + "     " + str(record[7]) + "\n"

        show = Label(root, width=120,  text=print_records, anchor=NW)
        show.grid(row=2, column=3, columnspan=2)
        show.place(x=402, y=50, height=510)
        show.config(font=("TimesNewRoman", 10))

    conn.commit()
    conn.close()
JacksonPro
  • 3,135
  • 2
  • 6
  • 29
Rocky Sotto
  • 29
  • 1
  • 7
  • Store data? As in storing data in a new file? Or just showing data? Anyway `ttk.Treeview` is worth a look, rather just using label. – Delrius Euphoria Feb 23 '21 at 04:35
  • I'm new in python and in this kind of community, I just want to create a system that can store a lot of records and to display it also in the interface. because i dont know how to connect treeview widget to Entry widget and database. can you help me? – Rocky Sotto Feb 23 '21 at 04:54

2 Answers2

0

Here is a very simple example of using ttk.Treeview to display your data from database:

def query():
    # Same code above...
    records = [('Hello','World'),('This','is'),('Treeview','widget')] # This will c.fetchall()
    
    cols = ('First column','Second columns') # Change this with whatever you want your column name to be
    tree = ttk.Treeview(root,columns=cols,show='headings',height=100)
    tree.pack(padx=10,pady=10)

    for col in cols: # Set treeview attributes
        tree.heading(col,text=col)
        tree.column(col,width=100,anchor='center')

    for items in records: # Insert values inside the treeview
        tree.insert('','end',values=items)

Keep in mind that since this is a ttk widget, you will have to import it first:

from tkinter import ttk

If you want your column to have separate widths, then check this answer

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
  • Thankyou for this code. but my another problem is once i clicked query to show the record it duplicates all the records stored in database ? I want only to show and check the record if its added into the database, if what is the latest record added into it . – Rocky Sotto Feb 28 '21 at 12:49
  • @SpencerHolland Maybe it is the way you have implemented it, check [this](https://stackoverflow.com/a/65971555/13382000). – Delrius Euphoria Feb 28 '21 at 14:52
  • Thankyou @Cool Cloud :) – Rocky Sotto Mar 01 '21 at 04:05
0

enter image description here

Once I clicked the query to show in treeview widget, it duplicates all the records in database. How to remove duplicate records and show only the records that has been added in database?

def show(self):
    global record
    conn = sqlite3.connect('billing.db')
    c = conn.cursor()
    c.execute("SELECT *, oid FROM billing")
    self.records = c.fetchall()

    for items in self.records:
        self.table.insert('', 'end', values=items)
Rocky Sotto
  • 29
  • 1
  • 7