rbeatt
rbeatt

Reputation: 73

Writing to a .txt file using Python and SQLite3

I am currently working on a school project where I have to create a database application with a GUI using Python, Tkinter, and SQLite3. I am trying to create a function where the user will type the OrderID into a form, and the order details with the corresponding OrderID will be taken from the database file and inserted into a text file. I am receiving the following error upon attempting to run the code:

  File "c:\Users\Ryan\OneDrive - C2k\A2 2020-2021\Computer Science\A2 Unit 5\Code\OrderForm.py", line 149, in PrintOrder
    write.write("-----CUSTOMER INVOICE-----", "\n".join(str(x) for x in results))
TypeError: write() takes exactly one argument (2 given)

A snippet of my code and a screenshot of the form is attached below:

    def PrintOrder(self):
        orderid = self.OrderIDEntry.get()
        productid = self.ProductIDEntry.get()
        quantity = self.QuantityEntry.get()
        with sqlite3.connect("LeeOpt.db") as db:
            cursor = db.cursor()
            search_order = ('''SELECT * FROM Orders WHERE OrderID = ?''')
            cursor.execute(search_order, [(orderid)])
            results = cursor.fetchall()

            if results:
                for i in results:
                    write = open("orderinvoice.txt","w")
                    write.write("-----CUSTOMER INVOICE-----", "\n".join(str(x) for x in results))
                    tkinter.messagebox.showinfo("Notification","Invoice generated successfully.")
                    self.ClearEntries()
            else:
                tkinter.messagebox.showerror("Error", "No order was found with this OrderID, please try again.")
                self.ClearEntries()

Order Form Screenshot

Upvotes: 1

Views: 507

Answers (1)

Niels Henkens
Niels Henkens

Reputation: 2696

Your problem/error lies in the line write.write("-----CUSTOMER INVOICE-----", "\n".join(str(x) for x in results)). The comma in write.write() creates 2 arguments, where the write-function expects only one (the text to write). I've adapted your code, so it still writes all results to seperate lines.

def PrintOrder(self):
    orderid = self.OrderIDEntry.get()
    productid = self.ProductIDEntry.get()
    quantity = self.QuantityEntry.get()
    with sqlite3.connect("LeeOpt.db") as db:
        cursor = db.cursor()
        search_order = ('''SELECT * FROM Orders WHERE OrderID = ?''')
        cursor.execute(search_order, [(orderid)])
        results = cursor.fetchall()

        if results:
            for i in results:
                write = open("orderinvoice.txt","w")
                write.write("-----CUSTOMER INVOICE-----")
                for x in results:
                    write.write(str(x))
                tkinter.messagebox.showinfo("Notification","Invoice generated successfully.")
                self.ClearEntries()
        else:
            tkinter.messagebox.showerror("Error", "No order was found with this OrderID, please try again.")
            self.ClearEntries()

Upvotes: 2

Related Questions