tinydb icon indicating copy to clipboard operation
tinydb copied to clipboard

truncate() has no effect

Open tollis-hunt opened this issue 6 years ago • 3 comments

Am I using this correctly? I would like it to clear all tables at the very beginning of the program. That way it is always empty on start. But as I run this it continuously appends the file with new entries.

from tinydb import TinyDB
from matplotlib import style
from matplotlib.figure import Figure

LARGE_FONT = ("Verdana", 12)
style.use("ggplot")

f = Figure(figsize=(5, 5), dpi=100)
a = f.add_subplot(111)

values = ("400", "401", "402")

db = TinyDB("db.json")
db.truncate()
table = db.table("RunData")


class Dashboard(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)

        container = Frame(self, borderwidth=5)
        container.grid_rowconfigure(0, weight=1, pad=15)
        container.grid_columnconfigure(0, weight=1, pad=15)

        self.serial_1 = tk.StringVar()
        self.controller_1 = tk.StringVar()
        self.sensitivity_1 = tk.IntVar()
        self.sensitivity_1.set(10)
        self.port_1 = tk.BooleanVar()

        tk.Label(text="Port").grid(row=0, column=0)
        tk.Label(text="Serial").grid(row=0, column=1)
        tk.Label(text="Controller").grid(row=0, column=2)
        tk.Label(text="Sensitivity").grid(row=0, column=3)

        self.port_1_check = ttk.Checkbutton(text="Port 1", var=self.port_1)
        self.port_1_check.grid(row=1, column=0)

        self.serial_entry = tk.Entry(textvariable=self.serial_1)
        self.serial_entry.insert(0, "20D0001")
        self.serial_entry.grid(row=1, column=1)

        self.controller_entry = ttk.Combobox(textvariable=self.controller_1, values=values)
        self.controller_entry.set(values[1])
        self.controller_entry.grid(row=1, column=2)

        self.sensitivity_lbl = ttk.Label(textvariable=self.sensitivity_1)
        self.sensitivity_lbl.grid(row=1, column=3)

        self.start_cal = ttk.Button(text="Begin", command=lambda: self.on_submit())
        self.start_cal.grid(row=2, column=0)

    def on_submit(self):
        if self.port_1:
            port_1 = {"port": 1, "serial": self.serial_1.get(), "controller": self.controller_1.get(),
                      "sensitivity": self.sensitivity_1.get()}
            table.insert(port_1)
        else:
            pass

        import json

        with open('db.json') as file:
            data = json.load(file)

        print("Run Data\n")
        print(json.dumps(data, indent=4, sort_keys=True))


class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        container = tk.Frame(self, borderwidth=5)
        container.grid_rowconfigure(0, weight=1, pad=15)
        container.grid_columnconfigure(0, weight=1, pad=15)

        self.Dashboard = Dashboard(self)
        self.Dashboard.grid(row=0, column=0)
        self.Dashboard.pack(side="right", fill="both", expand=True)


if __name__ == "__main__":
    root = tk.Tk()
    app = MainApplication(root)
    app.mainloop()

Let me know what I am doing wrong, thanks :)

tollis-hunt avatar Jun 09 '20 19:06 tollis-hunt

Hey @MrSassyBritches,

db.truncate() will truncate the default table, not all tables. To truncate the RunData table, you'd have to call table.truncate(). You also can call db.drop_tables(), then all tables will be removed from the database.

msiemens avatar Jun 10 '20 09:06 msiemens

tada, it worked! thanks for the help :)

tollis-hunt avatar Jun 10 '20 19:06 tollis-hunt

Glad I could help :)

msiemens avatar Jun 11 '20 07:06 msiemens