For our proyect we needed several tables. Each of them are explained ahead:
The table Users, described, looks like this:
Field | Type | NULL | Key | Default | Extra |
---|---|---|---|---|---|
username | VARCHAR(255) | NO | PRI | NULL | |
password | VARCHAR(255) | NO | NULL | ||
name | VARCHAR(255) | NO | NULL | ||
VARCHAR(255) | YES | NULL | |||
privileges | TINYINT(4) | YES | 1 | ||
submits | INT | YES | 0 |
The description of the items table, looks like this:
Field | Type | NULL | Key | Default | Extra |
---|---|---|---|---|---|
barcode | BIGINT | NO | PRI | NULL | |
name | VARCHAR(255) | NO | NULL | ||
brand | VARCHAR(255) | NO | NULL | ||
quantity | VARCHAR(255) | YES | NULL | ||
description | VARCHAR(500) | YES | NULL | ||
image | BINARY(255) | YES | NULL |
Field | Type | NULL | Key | Default | Extra |
---|---|---|---|---|---|
store_id | INT(11) | NO | PRI | AUTO_INCREMENT | |
name | VARCHAR(255) | NO | |||
location | VARCHAR(255) | NO | |||
latitude | DOUBLE | NO | |||
longitude | DOUBLE | NO |
The description of the table Lists_users looks like this:
Field | Type | NULL | Key | Default | Extra |
---|---|---|---|---|---|
list_id | INT(11) | NO | PRI | NULL | AUTO_INCREMENT |
username | VARCHAR(255) | NO | MUL | NULL | |
listname | VARCHAR(255) | NO | NULL |
Field | Type | NULL | Key | Default | Extra |
---|---|---|---|---|---|
list_id | INT(11) | NO | MUL | NULL | |
barcode | BIGINT(20) | NO | MUL | NULL |
Field | Type | NULL | Key | Default | Extra |
---|---|---|---|---|---|
barcode | BIGINT | NO | MUL | NULL | |
store_id | INT(11) | NO | MUL | NULL | |
price | double | NO | NULL |
This is the list of all the methods: It's important to note that, if a parameter has the same name in two or more tables, then we are talking about the same exact parameter
We're going to describe some of the most important methods we created for managing the Way_To_Shop database. These are many methods on our project, we're going to show the ones we consider the most representative of all, as they use most of the database actions, for example retrieving inserting and updating data.
def write_user(username, password, name, email): try: result = cursor.execute("INSERT INTO 'Users' ('username', 'password', 'name', 'mail') VALUES ( '%s' , '%s', '%s', '%s');" % (username, password , name, email)) db.commit() except: result = 0 return result
def add_submit(username): try: result = cursor.execute("UPDATE Users SET submits = submits + 1 WHERE username = '%s';" % username) db.commit() except: result = 0 return result
def modify_privileges(username, privileges): result = cursor.execute("UPDATE Users SET privileges=%s WHERE username = '%s';" % (privileges, username)) db.commit() return result
def write_item(barcode, name, brand, quantity, description, image): try: result= cursor.execute("INSERT INTO Items (barcode, name, brand, quantity, description, image) VALUES (%s, '%s', '%s', %s, '%s', '%s'); " % (barcode, name, brand, quantity, description, image)) db.commit() except: result = 0 return result
def get_coordinates(storeid): cursor.execute("SELECT latitude, longitude FROM Stores WHERE storeid= %s;" % storeid) return cursor.fetchone()
def get_stores(latitude, longitude, radius): cursor.execute("SELECT storeid, location, name FROM Stores WHERE (latitude > %s) AND (latitude < %s) AND (longitude > %s) AND (longitude < %s);" % ((latitude-radius), (latitude+radius), (longitude-radius), (longitude+radius))) return cursor.fetchall()
Note: As you can see, this is a little different from the ones we've seen. What we are doing is limiting latitude and longitude according to the radius. The limits are: latitude-radius < latitude < latitude + radius, and longitude-radius < longitude < longitude + radius
def delete_list(listid): try: result1 = cursor.execute("DELETE FROM Lists_items WHERE listid = %s;" % (listid)) result2 = cursor.execute("DELETE FROM Lists_users WHERE listid = %s;" % (listid)) result = [result1,result2] db.commit() except: result = [0,0] return result
Note: This is a method that affects two tables, so we execute the cursor for both, and define the final result as a list of two results.
def get_lowest_price(barcode): cursor.execute("SELECT storeid, price FROM Items_stores ORDER BY price ASC WHERE barcode = %s;" % barcode) return cursor.fetchone()
Note: We use the ORDER BY and ASC statements to put the lowest price as first, and then just fetch that one with fetchone().
def get_nearlowestprice(barcode, latitude, longitude, radius): cursor.execute ("SELECT price FROM Items_stores ORDER BY price LIMIT 1 WHERE barcode = %s AND (latitude > %s) AND (latitude < %s) AND (longitude > %s) AND (longitude < %s);"%(barcode, latitude-radius, latitude+radius, longitude-radius, longitude+radius)) return cursor.fetchone()