In: Computer Science
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
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.