Question: Create a script called manageProducts.py. It should allow the user to Create, Read, Update, and Delete products using the framework below. Products should have the

Create a script called manageProducts.py. It should allow the user to Create, Read, Update, and Delete products using the framework below. Products should have the following fields:

id (pk, auto_increment)

name

description

price

stock (number in inventory)

Create the product table as username_products

create a product class from the user class

change the code in app.py to use product class, not user

copy app.py to manageProduct.py, make changes so you can edit products instead of users.

add additional functionality to manageProducts.py to c,r,u,d

This means adding an if statement for read and delete, also finishing update.

import pymysql

import mysecrets

class baseObject:

def setup(self,tn):

self.tn = tn

self.conn = None

self.cur = None

self.fields = []

self.pk = None

self.data = [] #data is a list of dictionaries representing rows in our table

self.establishConnection()

self.getFields()

def establishConnection(self):

self.conn = pymysql.connect(host=mysecrets.db_host, port=3306, user=mysecrets.db_user,

passwd=mysecrets.db_passwd, db=mysecrets.db_name, autocommit=True)

self.cur = self.conn.cursor(pymysql.cursors.DictCursor)

def set(self,d):

self.data.append(d)

def getFields(self):

sql = f'''DESCRIBE `{self.tn}`;'''

self.cur.execute(sql)

for row in self.cur:

if 'auto_increment' in row['Extra']:

self.pk = row['Field']

else:

self.fields.append(row['Field'])

def insert(self,n=0):

count = 0

vals = []

sql = f"INSERT INTO `{self.tn}` ("

for field in self.fields:

sql += f"`{field}`,"

vals.append(self.data[n][field])

count +=1

sql = sql[0:-1] + ') VALUES ('

tokens = ("%s," * count)[0:-1]

sql += tokens + ');'

#print(sql,vals)

self.cur.execute(sql,vals)

self.data[n][self.pk] = self.cur.lastrowid

def getById(self,id):

sql = f"Select * from `{self.tn}` where `{self.pk}` = %s"

self.cur.execute(sql,(id))

self.data = []

for row in self.cur:

self.data.append(row)

def getAll(self):

sql = f"Select * from `{self.tn}`"

self.cur.execute(sql)

self.data = []

for row in self.cur:

self.data.append(row)

# UPDATE [tablename] SET [col] = [val] , .... WHERE [pk] = [our objects pk]

def update(self,n=0):

vals=[]

fvs=''

for field in self.fields:

if field in self.data[n].keys():

fvs += f"`{field}`=%s,"

vals.append(self.data[n][field])

fvs=fvs[:-1]

sql=f"UPDATE `{self.tn}` SET {fvs} WHERE `{self.pk}` = %s"

vals.append(self.data[n][self.pk])

#print(sql,vals)

self.cur.execute(sql,vals)

def deleteById(self,id):

sql = f"Delete from `{self.tn}` where `{self.pk}` = %s"

self.cur.execute(sql,(id))

from baseObject import baseObject

class Product(baseObject):

def __init__(self):

self.setup('demo_users')

def toList(self):

l = []

for row in self.data:

s = f"{row['name']} ({row['email']})"

l.append(s)

return l

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!