updatepush

This commit is contained in:
Mann Patel
2025-04-02 13:56:48 -06:00
parent 755069d279
commit a1ca7304eb
3 changed files with 86 additions and 21 deletions

View File

@@ -11,6 +11,7 @@ def database():
)
return db_connection
def get_all_products():
db_con = database()
@@ -37,32 +38,58 @@ def get_all_products():
results = cursor.fetchall()
print(results[1])
final = []
for row in results:
final.append(row)
print(row)
cursor.close()
db_con.close()
return final
def get_user_history():
def get_user_history(user_id):
db_con = database()
cursor = db_con.cursor()
user_id = 1
query= f"""select ProductID from History where UserID={user_id}"""
cursor.execute(query)
data = cursor.fetchall()
product_arr = []
for item in data:
product_arr.append(item[0])
querydata= tuple(product_arr)
prod_query = f"""select * from Product where ProductID IN {querydata} """
cursor.execute(prod_query)
res = cursor.fetchall()
for i in res:
print(i,"\n")
cursor.execute("SELECT CategoryID FROM Category")
categories = cursor.fetchall()
select_clause = "SELECT p.ProductID"
for category in categories:
category_id = category[0] # get the uid of the catefory and then append that to the new column
select_clause += f", MAX(CASE WHEN pc.CategoryID = {category_id} THEN 1 ELSE 0 END) AS `Cat_{category_id}`"
final_query = f"""
{select_clause}
FROM Product p
LEFT JOIN Product_Category pc ON p.ProductID = pc.ProductID
LEFT JOIN Category c ON pc.CategoryID = c.CategoryID
where p.ProductID in (select ProductID from History where UserID = {user_id})
GROUP BY p.ProductID;
"""
cursor.execute(final_query)
results = cursor.fetchall()
print(results[1])
final = []
for row in results:
final.append(row)
print(row)
cursor.close()
db_con.close()
return final
print("all products:")
get_all_products()
print("User History products:")
get_user_history(1)
def Calculate_cosin_similarity():
pass
get_user_history()
#get_all_products()
def Main:
pass

View File

@@ -0,0 +1,5 @@
select *
from Product
Where ProductID in (select ProductID
from History
where UserID=1);

View File

@@ -0,0 +1,33 @@
import mysql.connector as db_con
#TODO: Specify all the required queries
query_get_all_Prod= ("SELECT * FROM Product ")
#TODO: connect with the db
def database():
db = db_con.connect(
host = "localhost",
port = 3306,
user = "root",
database = "Marketplace"
)
cursor = db.cursor()
cursor.execute(query_get_all_Prod)
data = [None]
for item in cursor:
data.append(item)
# print(item)
print(data[1])
cursor.close()
db.close()
#TODO: Get All products
# Make it into a dictionary with product id and the list of category it would have
# {Prod1:[1,0,0,0,1]} this could mean its a [elctronics, 0,0,0, kitchen]
database()