Question

In: Computer Science

Create a project for this assignment. You can name it assignment02 if you wish. Download the...

Create a project for this assignment. You can name it assignment02 if you wish. Download the database file pizza-190807A.sqlite into the top level of the project.

Create the pizza_services.py module first and put in the code given in the assignment. Using this code ensures that you can use the services in a similar way to the example.

The assignment suggests adding a method customer to the class. This will return a list of rows from the customer table in the database. Go ahead and write this method. It will look very much like the method customer_list from the example.

Now create a Python script part1.py, no need to be subtle here. Set up a with statement similar to the example script, but for pizza. Call the customer method and print out what it returns. This is not exactly what you want, but you should see all the customer data. You will also see what order columns are provided in each row.

Now, write a loop that steps through the rows of data and prints one line for each row. Use formatting with column widths to get the different columns to line up properly.

Ive got this far essentially i need to print data from the database in such that it is the last name fallowed by the first name and then the email address for every row. im not sure if im going in the right direction with my code and just need to know how to read and print the names and info from the table

pizza service.py

import sqlite3

class PizzaServices:

    def __init__(self):
        self.connection = sqlite3.connect("pizza-190807A.sqlite")

    def __enter__(self):
        return self

    def __exit__(self, exe_type, exc_val, exl_tb):
        crs = self.connection.cursor()

    def do_query(self, query, parameters=None):
        crs = self.connection.cursor()
        if parameters:
            crs.execute(query, parameters)
        else:
            crs.execute(query)
            return crs.fetchall()

    def customer(self):
        return self.do_query("select * from customer")

part1.py

from pizza_services import PizzaServices

with PizzaServices() as cs:
    cust = cs.customer()
    cmd1 = "select FirstName, LastName, Email from customer"
    cs.do_query(cmd1)

templateH = "{:15}  {:15}  {:15}"
line = templateH.format("Last Name", "First Name", "email")
print(line)

for customer_row in cs:               //customer_row gives me errors for being not iterable
   //not sure what goes in here 

Solutions

Expert Solution

import sqlite3

class PizzaServices:

    def __init__(self):
        self.connection = sqlite3.connect("pizza-190807A.sqlite")

    def __enter__(self):
        return self

    def __exit__(self, exe_type, exc_val, exl_tb):
        crs = self.connection.cursor()

    def do_query(self, query, parameters=None):
        crs = self.connection.cursor()
        if parameters:
            crs.execute(query, parameters)
        else:
            crs.execute(query)
                return crs.fetchall()  # Do not indent it inside, it should execute in both if-else cases

    def customer(self):
        return self.do_query("select * from customer")
                
===============================================         
part1.py

from pizza_services import PizzaServices

with PizzaServices() as cs:
    cust = cs.customer()
    cmd1 = "select FirstName, LastName, Email from customer"
    resultSet = cs.do_query(cmd1)  # capture the result, It is a list of tuples

        templateH = "{:15}  {:15}  {:15}"
        line = templateH.format("Last Name", "First Name", "email")
        print(line)

        for customer_row in resultSet:               
                pritn('{:15}  {:15}  {:15}'.format(customer_row[0], customer_row[1], customer_row[2]))
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Stack Create a New Project and give your project a name, say. Download the given source...
Stack Create a New Project and give your project a name, say. Download the given source files StackArr.h and StackArr.cpp from Moodle and save them to your Lab6a folder. Also import them to your project. Add a source file to your project, called StackMain.cpp and implement your program according to the following: Prompt the user to input a program filename. Open the file and check if every right brace (i.e. }), bracket (i.e. ]), and parenthesis (i.e. )) in the...
Part 1: Stack Create a New Project and give your project a name, say Lab6a. Download...
Part 1: Stack Create a New Project and give your project a name, say Lab6a. Download the given source files StackArr.h and StackArr.cpp from Moodle and save them to your Lab6a folder. Also import them to your project. Add a source file to your project, called StackMain.cpp and implement your program according to the following: Prompt the user to input a program filename. Open the file and check if every right brace (i.e. }), bracket (i.e. ]), and parenthesis (i.e....
Assignment 5: MS Project Scheduling and Salvaging a Project Go to iCampus to download a free...
Assignment 5: MS Project Scheduling and Salvaging a Project Go to iCampus to download a free version of Microsoft Project needed to complete this assignment. Details on how to download the software are available in the online course shell in the Additional Resources section of the Student Center. Make the needed corrections to Assignment 3 using instructor feedback and initialize a new project in Microsoft Project to complete the following: 1. Create a work breakdown structure in MS Project using...
For this assignment, You will create a NodeJS application which takes a city name as an...
For this assignment, You will create a NodeJS application which takes a city name as an input in its pug template. using openweathermap API, you should get the weather of that particular city and displays that information to a new pug template. You should also store the results in a file in your directory. Following are the detailed requirements. Your application should start with a pug template (similar to HTML page) which has a form with an input field and...
You wish to create a cylindrical can that can hold a volume of 475 mL. The...
You wish to create a cylindrical can that can hold a volume of 475 mL. The cost of the material to make the top and bottom the can cost twice as much as the siding. Find the dimensions of the can such that the cost of the material is minimized. Submit your answer as an exact value. Exact Value being a calculator ready answer, not a specific value. This calculator ready answer CAN't CONTAIN THE SYMBOL OF PI. Radius (cm):...
Download this assignment in word and type answers directly below the questions. 1. Name the federal...
Download this assignment in word and type answers directly below the questions. 1. Name the federal agency charged with enforcement of employment discrimination. 2.Name the California agency charged with enforcement of employment discrimination. 3.What is disparate-impact discrimination? 4.What is disparate-treatment discrimination? 5.What is the difference between disparate-impact discrimination v. disparate -treatment discrimination? 6.Name the laws and the people that they protect (See EEOC presentation). 7. What are some of the defenses to alleged acts of discrimination.
You must create a project in intelliJ that you will name midterm_<yourLastname>. Inside the project you...
You must create a project in intelliJ that you will name midterm_<yourLastname>. Inside the project you must implement the following packages and classes as they appear in the following description: A public class called Square in a package it200.midterm.shapes. The class contains the following: An int field called side, that stores the size of each side of the square (in terms of number of characters) A char field called symbol, which stores a character as the symbol we use to...
This is an assignment for python. Download the code “ GradeBook.py ” You will be modifying...
This is an assignment for python. Download the code “ GradeBook.py ” You will be modifying the code to do the following: 1) create an empty dictionary named “FinalAverages” 2) In one for loop you will zip all lists together and get their individual members on each iteration. You can name these what ever you want. 3) on each iteration: Calculate the WEIGHTED average using the weights provided, and then add a new dictionary value to the dictionary “ FinalAverages...
In this assignment you create a small project with a base class 'Account' which represents a...
In this assignment you create a small project with a base class 'Account' which represents a generic account independent of whether the account is a Client or Vendor or Bank account. Then we will define a derived class 'BankAccount' class that represents the bank account. The BankAccount class is derived from the Account class, and extends the Account class functionality. The functionality of the BankAccount class is to handle deposits and withdrawals. You create a base class called Account and...
Process Cost Excel Project Create a new Excel spreadsheet and name it “Last name_PC”. You project...
Process Cost Excel Project Create a new Excel spreadsheet and name it “Last name_PC”. You project is to create a model for a production cost report using the weighted average method for the month of May.   Following good Excel design techniques, you should have an input area in which you put the department information for the month, and an output area that calculates the production cost report. As always, you should have only formulas or references in your output area....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT