1

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

2
  • What is the error message telling you? What are you actually doing? Have you tried doing what it wants you to do in the error message instead? Commented Mar 8, 2021 at 21:26
  • @PranavHosangadi I need to write the data from the field from the database file. I'm not sure how to get around it. Commented Mar 8, 2021 at 21:33

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

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.