How do I fix the error: sqlite3.Connection' object has no attribute 'fetchone' in sqlite query

3.6k Views Asked by At

I am having a bit of a headache with this code. When I write 'A' in the input prompt (SQL2) it seems to work fine, however when I write 'D', (SQL1) I get the error:

sqlite3.Connection' object has no attribute 'fetchone

I am not very confident that I am answering the task either because the SQL1 gives me zero output. So I`ll post the task text and my code. Sorry for the wall of text.

The task:

retrieve the list of products name, the name and the last name of the customer(s) who ordered these products, the order's order number, order date and total amount, and the product's supplier name

the results must only be included if the supplier's company name starts with "E". The results must be sorted based on the supplier's company name. The supplier's company name must be sorted alphabetically (A-Z).

Once you have the retrieved information you must display the appropriate message and result set. If the user wanted to see the results when the products were available, only display(print) the number of rows which were returned by the query

If the user wanted to see the results when the products were discontinued, only display(print) the number of rows which were returned by the query. If the user entered an incorrect option then the following code should be used to display the incorrect input message, including the incorrect input. The code to do this is provided below print

("Invalid input{selection}.".format(selection = DiscOpt))

The Code:

       import sqlite3
       conn = sqlite3.connect('Northwind2020.db')
       cursor = conn.cursor()
       DiscOpt = input("Would you like to see customers who ordered available or discontinued
       products.")
       print("Type 'A' for available products. Type 'D' for discontinued products.")

       SQL1 = '''SELECT P.ProductName, C.FirstName, C.LastName, O.OrderNumber, O.OrderDate, 
                 O.TotalAmount, S.CompanyName
                 FROM Customer AS C
                 INNER JOIN [Order] AS O
                 ON C.Id = O.CustomerId
                 INNER JOIN OrderItem AS OI
                 ON O.Id = OI.OrderId
                 INNER JOIN Product AS P
                 ON P.Id = OI.ProductId
                 INNER JOIN Supplier AS S
                 ON S.Id = P.SupplierId
                 WHERE S.CompanyName LIKE 'E%'
                 AND P.IsDiscontinued = 1
                 ORDER BY S.CompanyName ASC;'''

      SQL2 = '''SELECT P.ProductName, C.FirstName, C.LastName, O.OrderNumber, O.OrderDate, 
                O.TotalAmount, S.CompanyName
                FROM Customer AS C
                INNER JOIN [Order] AS O
                ON C.Id = O.CustomerId
                INNER JOIN OrderItem AS OI
                ON O.Id = OI.OrderId
                INNER JOIN Product AS P
                ON P.Id = OI.ProductId
                INNER JOIN Supplier AS S
                ON S.Id = P.SupplierId
                WHERE S.CompanyName LIKE 'E%'
                AND P.IsDiscontinued = 0
                ORDER BY S.CompanyName ASC;'''

     while True:
         if DiscOpt == 'A':
             for row in cursor.execute(SQL2).fetchall():
                 print(row)
     elif DiscOpt == 'D':
             cursor = conn.cursor()
             for row in cursor.execute(SQL1).fethcall():
                 print(row)
     else:
          "Invalid input{selection}.".format(selection = DiscOpt) 

`

1

There are 1 best solutions below

0
mj_from_the_north On

It was a stupid spelling mistake that made my code misfunction, Its actually even spelled in the title (fethcone instead of fetchone).I was just to tired to see it. But I got some help to write it in a not so dense and repetitive way. So I`ll post it below:

    import sqlite3
    conn = sqlite3.connect('Northwind2020.db')
    cursor = conn.cursor()
    DiscOpt = input("Would you like to see customers who ordered available or 
                     discontinued products.")
    print("Type 'A' for available products. Type 'D' for discontinued 
          products.")

    SQL = '''SELECT P.ProductName, C.FirstName, C.LastName, O.OrderNumber, 
                     O.OrderDate, O.TotalAmount, S.CompanyName
                     FROM Customer AS C
                     INNER JOIN [Order] AS O ON C.Id = O.CustomerId
                     INNER JOIN OrderItem AS OI ON O.Id = OI.OrderId
                     INNER JOIN Product AS P ON P.Id = OI.ProductId
                     INNER JOIN Supplier AS S ON S.Id = P.SupplierId
                     WHERE S.CompanyName LIKE 'E%'
                        AND P.IsDiscontinued = ?
                     ORDER BY S.CompanyName ASC;'''

    while True:
        if DiscOpt == 'A':
            for row in cursor.execute(SQL, (0,)).fetchall():
                print(row)
        elif DiscOpt == 'D':
             for row in cursor.execute(SQL, (1,)).fetchall():
                print(row)
        else:
             "Invalid input{selection}.".format(selection = DiscOpt)