From b5ef9befee5f90886ed0325fdeadfabebb5a0954 Mon Sep 17 00:00:00 2001 From: AlessandroNatoli <115617867+AlessandroNatoli@users.noreply.github.com> Date: Fri, 2 Dec 2022 09:00:34 +0100 Subject: [PATCH 01/13] aggiunto applicazione di python --- program_database.py | 183 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 program_database.py diff --git a/program_database.py b/program_database.py new file mode 100644 index 0000000..f8d696e --- /dev/null +++ b/program_database.py @@ -0,0 +1,183 @@ +def load_data(customer_database): + import pandas as pd + data = pd.read_csv('customer_segmentation.csv') + + + import mysql.connector as mysql + from mysql.connector import Error + + db_name='CustomersDatabase1' + try: + mydb = mysql.connect(host='localhost', user='Alessandro', password='Terracina1!', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + if mydb.is_connected(): + mycursor = mydb.cursor() + mycursor.execute('SHOW DATABASES') + result = mycursor.fetchall() + print(result) + for x in result: + if db_name == x[0]: + mycursor.execute('DROP DATABASE ' + db_name) # delete old database + mydb.commit() # make the changes official + print("The database already exists! The old database has been deleted!)") + + mycursor.execute("CREATE DATABASE "+ db_name) + print("Database is created") + except Error as e: + print("Error while connecting to MySQL", e) + + mycursor.execute("USE CustomersDatabase1") + + mycursor.execute( + ''' + CREATE TABLE customer( + customer_unique_id VARCHAR(40), + customer_id VARCHAR(40), + customer_city VARCHAR(40), + customer_state VARCHAR(40), + PRIMARY KEY (customer_unique_id) + ); + ''' + ) + + mycursor.execute( + ''' + CREATE TABLE seller( + seller_id VARCHAR(40), + seller_city VARCHAR(40), + seller_state VARCHAR(40), + PRIMARY KEY (seller_id) + ); + + + ''' + ) + + + mycursor.execute( + ''' + CREATE TABLE product( + product_id VARCHAR(40), + price int, + freight_value int, + product_category_name VARCHAR(60), + product_description_lenght int, + PRIMARY KEY (product_id) + ); + + + ''' + ) + + + mycursor.execute( + ''' + CREATE TABLE order_( + order_id VARCHAR(40), + order_status VARCHAR(40), + order_purchase_time DATETIME, + order_approved_at DATETIME, + order_delivered_carrier_date DATETIME, + order_delivered_customer_date DATETIME, + order_estimated_delivery_date DATETIME, + payment_type VARCHAR(40), + payment_value int, + payment_installments int, + order_customer VARCHAR(40), + order_product VARCHAR(40), + order_seller VARCHAR(40), + PRIMARY KEY (order_id), + FOREIGN KEY (order_customer) REFERENCES customer(customer_unique_id), + FOREIGN KEY (order_seller) REFERENCES seller(seller_id), + FOREIGN KEY (order_product) REFERENCES product(product_id) + ); + + ''' + ) + mycursor.execute( + ''' + CREATE TABLE offer( + id VARCHAR(40), + seller_id VARCHAR(40), + product_id VARCHAR(40), + PRIMARY KEY (id), + FOREIGN KEY (seller_id) REFERENCES seller(seller_id), + FOREIGN KEY (product_id) REFERENCES product(product_id) + ); + ''' + ) + for i,row in data.iterrows(): + sql = "INSERT IGNORE INTO CustomersDatabase1.customer VALUES (%s,%s,%s,%s)" + mycursor.execute(sql, tuple([row['customer_unique_id'], row['customer_id'], row['customer_city'], row['customer_state']])) + #print("Record inserted") + mydb.commit() + for i,row in data.iterrows(): + sql = "INSERT IGNORE INTO CustomersDatabase1.seller VALUES (%s,%s,%s)" + mycursor.execute(sql, tuple([row['seller_id'], row['seller_city'], row['seller_state']])) + #print("Record inserted") + mydb.commit() + for i,row in data.iterrows(): + sql = "INSERT IGNORE INTO CustomersDatabase1.product VALUES (%s,%s,%s,%s,%s)" + mycursor.execute(sql, tuple([row['product_id'], row['price'], row['freight_value'], row['product_category_name'], row['product_description_lenght']])) + #print("Record inserted") + mydb.commit() + + for i,row in data.iterrows(): + sql = "INSERT IGNORE INTO CustomersDatabase1.order_ VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" + mycursor.execute(sql, tuple([row['order_id'], row['order_status'], row['order_purchase_timestamp'], row['order_approved_at'], row['order_delivered_carrier_date'], row['order_delivered_customer_date'], row['order_estimated_delivery_date'], row['payment_type'], row['payment_value'], row['payment_installments'], row['customer_unique_id'], row['product_id'] , row['seller_id']])) + #print("Record inserted") + mydb.commit() + df2 = data.loc[:,['product_id','seller_id']] + df2.drop_duplicates(inplace = True) + c = 0 + for i,row in df2.iterrows(): + sql = "INSERT IGNORE INTO CustomersDatabase1.offer VALUES (%s,%s,%s)" + mycursor.execute(sql, tuple([c, row['seller_id'], row['product_id']])) + c+=1 + #print("Record inserted") + mydb.commit() + + + + + + print("I loaded the dataset and built the database!\n") + +def query1(): + """inser sql code""" +def query2(): + """inser sql code""" +def query3(): + """inser sql code""" +def query4(): + """inser sql code""" +def query5(): + """inser sql code""" + +if __name__=="__main__": + print("Welcome to our project!\n") + load_data("customer_segmentation.csv") + + valid_choices=['insert ','quit'] + + while True: + choice=input('''\n\nChoose query to execute bla bla bla''') + + if choice not in valid_choices: + print(f"Your choice '{choice}' is not valid. Please insert a valid option") + continue + if choice=="quit": + break + print(f"\nYou chose to execute query {choice}") + if choice == "": + continue + elif choice == "": + continue + elif choice == "": + continue + elif choice == "": + continue + elif choice == "": + continue + else: + raise Exception("Something went really wrong") + print("\nGoodbye!\n") From 2e4f210e220937433fed9671d07c23bd38f1981e Mon Sep 17 00:00:00 2001 From: tommasoagudio Date: Fri, 2 Dec 2022 12:49:27 +0100 Subject: [PATCH 02/13] query 1-2 done queries 1,2 --- program_database.py | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/program_database.py b/program_database.py index f8d696e..f2aa0ce 100644 --- a/program_database.py +++ b/program_database.py @@ -8,7 +8,7 @@ def load_data(customer_database): db_name='CustomersDatabase1' try: - mydb = mysql.connect(host='localhost', user='Alessandro', password='Terracina1!', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) if mydb.is_connected(): mycursor = mydb.cursor() mycursor.execute('SHOW DATABASES') @@ -143,9 +143,39 @@ def load_data(customer_database): print("I loaded the dataset and built the database!\n") def query1(): - """inser sql code""" + import mysql.connector as mysql + from mysql.connector import Error + mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = (mycursor.execute( + ''' + select product_category_name,count(product_category_name) from product + GROUP BY + product_category_name + ORDER BY count(product_category_name) desc + ''' + )) + mycursor.execute(sql) + result = mycursor.fetchall() + print(result) def query2(): - """inser sql code""" + import mysql.connector as mysql + from mysql.connector import Error + mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = (mycursor.execute( + ''' + select count(c.customer_city), c.customer_city from customer as c, order_ as o + where c.customer_unique_id = o.order_customer + group by c.customer_city + order by count(c.customer_city) DESC; + ''' + )) + mycursor.execute(sql) + result = mycursor.fetchall() + print(result) def query3(): """inser sql code""" def query4(): From 12b9d93ce9bab0bdb2cfabd689a80fcdbb683340 Mon Sep 17 00:00:00 2001 From: tommasoagudio Date: Sat, 3 Dec 2022 17:02:48 +0100 Subject: [PATCH 03/13] added queries --- program_database.py | 92 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 8 deletions(-) diff --git a/program_database.py b/program_database.py index f2aa0ce..5af4979 100644 --- a/program_database.py +++ b/program_database.py @@ -142,7 +142,7 @@ def load_data(customer_database): print("I loaded the dataset and built the database!\n") -def query1(): +def query1(): #returns the number of orders for each product import mysql.connector as mysql from mysql.connector import Error mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) @@ -159,7 +159,7 @@ def query1(): mycursor.execute(sql) result = mycursor.fetchall() print(result) -def query2(): +def query2() : #returns the number of orders for each city import mysql.connector as mysql from mysql.connector import Error mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) @@ -176,12 +176,88 @@ def query2(): mycursor.execute(sql) result = mycursor.fetchall() print(result) -def query3(): - """inser sql code""" -def query4(): - """inser sql code""" -def query5(): - """inser sql code""" +def query3(): #returns the number of orders for each customer + import mysql.connector as mysql + from mysql.connector import Error + mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = (mycursor.execute( + ''' + select count(c.customer_id),C.customer_id from customer as c, order_ as o + where c.customer_unique_id = o.order_customer + group by c.customer_id + order by count(c.customer_unique_id) DESC; + ''' + )) + mycursor.execute(sql) + result = mycursor.fetchall() + print(result) +def query4(): #returns the avarage number of installments + import mysql.connector as mysql + from mysql.connector import Error + mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = (mycursor.execute( + ''' + select avg(o.payment_installments) from order_ as o; + + ''' + )) + mycursor.execute(sql) + result = mycursor.fetchall() + print(result) +def query5(): #returns the payment value, price and installments for each order. + import mysql.connector as mysql + from mysql.connector import Error + mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = (mycursor.execute( + ''' + select c.customer_id, o.payment_value, o.payment_installments, o.payment_type from order_ as o, customer as c + where c.customer_unique_id = o.order_customer + group by c.customer_id, o.payment_installments, o.payment_value,o.payment_type; + ''' + )) + mycursor.execute(sql) + result = mycursor.fetchall() + print(result) + +def query6(): #number of orders for a specific city and a specific payment_type + + import mysql.connector as mysql + from mysql.connector import Error + mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = (mycursor.execute( + ''' + select count(c.customer_city), c.customer_city, o.payment_type from customer as c, order_ as o + where c.customer_unique_id = o.order_customer and c.customer_city = 'sao paulo' and o.payment_type = 'boleto'; + + ''' + )) + mycursor.execute(sql) + result = mycursor.fetchall() + print(result) + +def query7(): #return the avarage payment value for a specific payment type + import mysql.connector as mysql + from mysql.connector import Error + mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = (mycursor.execute( + ''' + select round(avg(o.payment_value)), o.payment_type from order_ as o + where payment_type = 'boleto'; + ''' + )) + mycursor.execute(sql) + result = mycursor.fetchall() + print(result) if __name__=="__main__": print("Welcome to our project!\n") From 93cc76c5edcf58f23c9de1dbdb6c0de423d4f312 Mon Sep 17 00:00:00 2001 From: tommasoagudio Date: Sun, 4 Dec 2022 15:53:40 +0100 Subject: [PATCH 04/13] added queries and fixed some errors --- .DS_Store | Bin 0 -> 8196 bytes program_database.py | 420 ++++++++++++++++++++++++++++++++------------ 2 files changed, 312 insertions(+), 108 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e931a8c2a695946ae2e0d22f9271188a8612aa41 GIT binary patch literal 8196 zcmeI1%Wl&^6o&sPT@-Zz5}UF>@)9HzwT;rtE>!7)1siyP5+_a*np<;enym5&JOQu3 z^Ra? PuSn>j)NS%>pPBfnXoHPG9GuCk=nTvV*NIH7eM0s|S-nO;Tc*7|!#+E=SfAxsGKFmxiD8^X_7r5mz^;r7RGvFD>Ghl1?*J^1_#Xd{b?~$%uH9us&N$>G=s0wSRy8GB4 zwZi@=1Z@x82YThVYy6U4so^k9g~%!21g<#GZ*`=1Xy|FICAz|lmY*3dFE(f?L>s%4 zv=3vxM2y(Q1Rn-?(9ZZ&v3{g|FyGPg{?SgJ>$5J|Uqhde74)<-h+i4I`*1Zt^P!Ga zg{@QFYo(Jk$}#rik(~!rOwYi<6$n!930=al7VNjR|mHKzbyX#zm>jwr#u6m zfq%n**=>AjT%aqdt#ev!tsT(6(rvTr*0FRD23L+FT{({Y`-dUF1D0A$J(25JGDG|C U9|HXQUwWuE{QKYK;8Ps_0K>-@x&QzG literal 0 HcmV?d00001 diff --git a/program_database.py b/program_database.py index 5af4979..56116a0 100644 --- a/program_database.py +++ b/program_database.py @@ -1,26 +1,36 @@ def load_data(customer_database): import pandas as pd - data = pd.read_csv('customer_segmentation.csv') - + data = pd.read_csv("customer_segmentation.csv") import mysql.connector as mysql - from mysql.connector import Error + from mysql.connector import Error + + global username + username = input("Please enter your mysql username: ") + global password + password = input("Please now enter your mysql password: ") - db_name='CustomersDatabase1' + db_name = "CustomersDatabase1" try: - mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) # you can add the auth_plugin here too (ref line 26) if mydb.is_connected(): mycursor = mydb.cursor() - mycursor.execute('SHOW DATABASES') + mycursor.execute("SHOW DATABASES") result = mycursor.fetchall() print(result) for x in result: if db_name == x[0]: - mycursor.execute('DROP DATABASE ' + db_name) # delete old database - mydb.commit() # make the changes official - print("The database already exists! The old database has been deleted!)") - - mycursor.execute("CREATE DATABASE "+ db_name) + # mycursor.execute('DROP DATABASE ' + db_name) # delete old database + # mydb.commit() # make the changes official + print("The database already exists!") + return None + + mycursor.execute("CREATE DATABASE IF NOT EXISTS" + db_name) print("Database is created") except Error as e: print("Error while connecting to MySQL", e) @@ -28,7 +38,7 @@ def load_data(customer_database): mycursor.execute("USE CustomersDatabase1") mycursor.execute( - ''' + """ CREATE TABLE customer( customer_unique_id VARCHAR(40), customer_id VARCHAR(40), @@ -36,11 +46,11 @@ def load_data(customer_database): customer_state VARCHAR(40), PRIMARY KEY (customer_unique_id) ); - ''' + """ ) mycursor.execute( - ''' + """ CREATE TABLE seller( seller_id VARCHAR(40), seller_city VARCHAR(40), @@ -49,12 +59,11 @@ def load_data(customer_database): ); - ''' + """ ) - mycursor.execute( - ''' + """ CREATE TABLE product( product_id VARCHAR(40), price int, @@ -65,12 +74,11 @@ def load_data(customer_database): ); - ''' + """ ) - mycursor.execute( - ''' + """ CREATE TABLE order_( order_id VARCHAR(40), order_status VARCHAR(40), @@ -91,10 +99,10 @@ def load_data(customer_database): FOREIGN KEY (order_product) REFERENCES product(product_id) ); - ''' - ) + """ + ) mycursor.execute( - ''' + """ CREATE TABLE offer( id VARCHAR(40), seller_id VARCHAR(40), @@ -103,186 +111,382 @@ def load_data(customer_database): FOREIGN KEY (seller_id) REFERENCES seller(seller_id), FOREIGN KEY (product_id) REFERENCES product(product_id) ); - ''' + """ ) - for i,row in data.iterrows(): + for i, row in data.iterrows(): sql = "INSERT IGNORE INTO CustomersDatabase1.customer VALUES (%s,%s,%s,%s)" - mycursor.execute(sql, tuple([row['customer_unique_id'], row['customer_id'], row['customer_city'], row['customer_state']])) - #print("Record inserted") + mycursor.execute( + sql, + tuple( + [ + row["customer_unique_id"], + row["customer_id"], + row["customer_city"], + row["customer_state"], + ] + ), + ) + # print("Record inserted") mydb.commit() - for i,row in data.iterrows(): + for i, row in data.iterrows(): sql = "INSERT IGNORE INTO CustomersDatabase1.seller VALUES (%s,%s,%s)" - mycursor.execute(sql, tuple([row['seller_id'], row['seller_city'], row['seller_state']])) - #print("Record inserted") + mycursor.execute( + sql, tuple([row["seller_id"], row["seller_city"], row["seller_state"]]) + ) + # print("Record inserted") mydb.commit() - for i,row in data.iterrows(): + for i, row in data.iterrows(): sql = "INSERT IGNORE INTO CustomersDatabase1.product VALUES (%s,%s,%s,%s,%s)" - mycursor.execute(sql, tuple([row['product_id'], row['price'], row['freight_value'], row['product_category_name'], row['product_description_lenght']])) - #print("Record inserted") + mycursor.execute( + sql, + tuple( + [ + row["product_id"], + row["price"], + row["freight_value"], + row["product_category_name"], + row["product_description_lenght"], + ] + ), + ) + # print("Record inserted") mydb.commit() - - for i,row in data.iterrows(): + + for i, row in data.iterrows(): sql = "INSERT IGNORE INTO CustomersDatabase1.order_ VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" - mycursor.execute(sql, tuple([row['order_id'], row['order_status'], row['order_purchase_timestamp'], row['order_approved_at'], row['order_delivered_carrier_date'], row['order_delivered_customer_date'], row['order_estimated_delivery_date'], row['payment_type'], row['payment_value'], row['payment_installments'], row['customer_unique_id'], row['product_id'] , row['seller_id']])) - #print("Record inserted") + mycursor.execute( + sql, + tuple( + [ + row["order_id"], + row["order_status"], + row["order_purchase_timestamp"], + row["order_approved_at"], + row["order_delivered_carrier_date"], + row["order_delivered_customer_date"], + row["order_estimated_delivery_date"], + row["payment_type"], + row["payment_value"], + row["payment_installments"], + row["customer_unique_id"], + row["product_id"], + row["seller_id"], + ] + ), + ) + # print("Record inserted") mydb.commit() - df2 = data.loc[:,['product_id','seller_id']] - df2.drop_duplicates(inplace = True) + df2 = data.loc[:, ["product_id", "seller_id"]] + df2.drop_duplicates(inplace=True) c = 0 - for i,row in df2.iterrows(): + for i, row in df2.iterrows(): sql = "INSERT IGNORE INTO CustomersDatabase1.offer VALUES (%s,%s,%s)" - mycursor.execute(sql, tuple([c, row['seller_id'], row['product_id']])) - c+=1 - #print("Record inserted") + mycursor.execute(sql, tuple([c, row["seller_id"], row["product_id"]])) + c += 1 + # print("Record inserted") mydb.commit() - - - - print("I loaded the dataset and built the database!\n") -def query1(): #returns the number of orders for each product + +def query1(): # returns the number of orders for each product import mysql.connector as mysql from mysql.connector import Error - mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) # you can add the auth_plugin here too (ref line 26) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") - sql = (mycursor.execute( - ''' + sql = mycursor.execute( + """ select product_category_name,count(product_category_name) from product GROUP BY product_category_name ORDER BY count(product_category_name) desc - ''' - )) + """ + ) mycursor.execute(sql) result = mycursor.fetchall() - print(result) -def query2() : #returns the number of orders for each city + for element in result: + print(element) + + +def query2(): # returns the number of orders for each city import mysql.connector as mysql from mysql.connector import Error - mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) # you can add the auth_plugin here too (ref line 26) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") - sql = (mycursor.execute( - ''' + sql = mycursor.execute( + """ select count(c.customer_city), c.customer_city from customer as c, order_ as o where c.customer_unique_id = o.order_customer group by c.customer_city order by count(c.customer_city) DESC; - ''' - )) + """ + ) mycursor.execute(sql) result = mycursor.fetchall() - print(result) -def query3(): #returns the number of orders for each customer + for element in result: + print(element) + + +def query3(): # returns the number of orders for each customer import mysql.connector as mysql from mysql.connector import Error - mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) # you can add the auth_plugin here too (ref line 26) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") - sql = (mycursor.execute( - ''' + sql = mycursor.execute( + """ select count(c.customer_id),C.customer_id from customer as c, order_ as o where c.customer_unique_id = o.order_customer group by c.customer_id order by count(c.customer_unique_id) DESC; - ''' - )) + """ + ) mycursor.execute(sql) result = mycursor.fetchall() - print(result) -def query4(): #returns the avarage number of installments + for element in result: + print(element) + + +def query4(): # returns the avarage number of installments import mysql.connector as mysql from mysql.connector import Error - mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) # you can add the auth_plugin here too (ref line 26) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") - sql = (mycursor.execute( - ''' + sql = mycursor.execute( + """ select avg(o.payment_installments) from order_ as o; - ''' - )) + """ + ) mycursor.execute(sql) result = mycursor.fetchall() - print(result) -def query5(): #returns the payment value, price and installments for each order. + for element in result: + print(element) + + +def query5(): # returns the payment value, price and installments for each order. import mysql.connector as mysql from mysql.connector import Error - mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) # you can add the auth_plugin here too (ref line 26) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") - sql = (mycursor.execute( - ''' + sql = mycursor.execute( + """ select c.customer_id, o.payment_value, o.payment_installments, o.payment_type from order_ as o, customer as c where c.customer_unique_id = o.order_customer group by c.customer_id, o.payment_installments, o.payment_value,o.payment_type; - ''' - )) + """ + ) mycursor.execute(sql) result = mycursor.fetchall() - print(result) + for element in result: + print(element) -def query6(): #number of orders for a specific city and a specific payment_type +def query6(): # number of orders for a specific city and a specific payment_type import mysql.connector as mysql from mysql.connector import Error - mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + import pandas as pd + + data = pd.read_csv("customer_segmentation.csv") + possible_cities = [] + possible_payment_types = [] + for city in data["customer_city"]: + if city not in possible_cities: + possible_cities.append(city) + for payments in data["payment_type"]: + if payments not in possible_payment_types: + possible_payment_types.append(payments) + print("Possible cities to select: ", possible_cities) + print("") + print("Possible payment types to select: ", possible_payment_types) + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) # you can add the auth_plugin here too (ref line 26) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") - sql = (mycursor.execute( - ''' + + city = input("Please insert the desired city: ") + payment_type = input("Please insert the desired payment_type: ") + sql = mycursor.execute( + """ select count(c.customer_city), c.customer_city, o.payment_type from customer as c, order_ as o - where c.customer_unique_id = o.order_customer and c.customer_city = 'sao paulo' and o.payment_type = 'boleto'; + where c.customer_unique_id = o.order_customer and c.customer_city = %s and o.payment_type = %s; - ''' - )) + """, + (city, payment_type), + ) mycursor.execute(sql) result = mycursor.fetchall() - print(result) - -def query7(): #return the avarage payment value for a specific payment type + for element in result: + print(element) + + +def query7(): # return the avarage payment value for a specific payment type import mysql.connector as mysql from mysql.connector import Error - mydb = mysql.connect(host='localhost', user='root', password='Verazzano1', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26) + import pandas as pd + + data = pd.read_csv("customer_segmentation.csv") + possible_payment_types = [] + for payments in data["payment_type"]: + if payments not in possible_payment_types: + possible_payment_types.append(payments) + payment_type = list(input("please enter the desired payment type ")) + print("Possible payment types to select: ", possible_payment_types) + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) # you can add the auth_plugin here too (ref line 26) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") - sql = (mycursor.execute( - ''' + sql = mycursor.execute( + """ select round(avg(o.payment_value)), o.payment_type from order_ as o - where payment_type = 'boleto'; - ''' - )) + where o.payment_type = %s; + """, + (payment_type), + ) mycursor.execute(sql) result = mycursor.fetchall() - print(result) + for element in result: + print(element) + -if __name__=="__main__": +def query8(): + import mysql.connector as mysql + from mysql.connector import Error + import pandas as pd + + data = pd.read_csv("customer_segmentation.csv") + possible_cities = [] + for city in data["customer_city"]: + if city not in possible_cities: + possible_cities.append(city) + print("Possible cities to select: ", possible_cities) + + amount = int(input("please enter minimum amount \n")) + city = input("please enter the desired city \n") + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) # you can add the auth_plugin here too (ref line 26) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = mycursor.execute( + """ + select c.customer_id, sum(o.payment_value) as x, c.customer_city from customer as c, order_ as o + where c.customer_unique_id = o.order_customer and c.customer_city = %s + group by c.customer_id,o.payment_value, c.customer_city + having x > %s + order by o.payment_value DESC; + """, + (city, amount), + ) + mycursor.execute(sql) + result = mycursor.fetchall() + for element in result: + print(element) + + +if __name__ == "__main__": print("Welcome to our project!\n") load_data("customer_segmentation.csv") - valid_choices=['insert ','quit'] + valid_choices = ["1", "2", "3", "4", "5", "6", "7", "8", "quit"] while True: - choice=input('''\n\nChoose query to execute bla bla bla''') + print("possible choices: \n") + for choices in valid_choices: + print(choices) + choice = input("""\n\nPlease choose a query to run!\n""").lower() if choice not in valid_choices: print(f"Your choice '{choice}' is not valid. Please insert a valid option") continue - if choice=="quit": + if choice == "quit": break print(f"\nYou chose to execute query {choice}") - if choice == "": + if choice == "1": + print("This query will show the number of orders for each product \n ") + query1() + continue + elif choice == "2": + print("This query will show the number of orders for each city \n") + query2() + continue + elif choice == "3": + print("This query will show the number of orders for each customer \n") + query3() + continue + elif choice == "4": + print("This query will show the avarage number of installments \n") + query4() continue - elif choice == "": + elif choice == "5": + print( + "This query will show payment value, payment type and installments for each order \n" + ) + query5() continue - elif choice == "": + elif choice == "6": + print( + "This query will show the number of orders for your desired city and payment type" + ) + query6() continue - elif choice == "": + elif choice == "7": + print( + "This query will show the avarage payment value for the desired payment type" + ) + query7() continue - elif choice == "": + elif choice == "8": + print( + "This query will show the customers that have spent at least the desired amount and living in the determined city" + ) + query8() continue else: raise Exception("Something went really wrong") From 14ecc44a0d72783d96fcf6228e29ff898fbb7290 Mon Sep 17 00:00:00 2001 From: AlessandroNatoli Date: Sun, 4 Dec 2022 18:53:34 +0100 Subject: [PATCH 05/13] Update program_database.py sistemato un errore di scrittura e aggiunto \n --- program_database.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/program_database.py b/program_database.py index 56116a0..20630a9 100644 --- a/program_database.py +++ b/program_database.py @@ -472,19 +472,19 @@ def query8(): continue elif choice == "6": print( - "This query will show the number of orders for your desired city and payment type" + "This query will show the number of orders for your desired city and payment type \n" ) query6() continue elif choice == "7": print( - "This query will show the avarage payment value for the desired payment type" + "This query will show the average payment value for the desired payment type \n" ) query7() continue elif choice == "8": print( - "This query will show the customers that have spent at least the desired amount and living in the determined city" + "This query will show the customers that have spent at least the desired amount and living in the determined city \n" ) query8() continue From 994fca3b78eeb061531a2221331db25602e25b73 Mon Sep 17 00:00:00 2001 From: tommasoagudio Date: Sun, 4 Dec 2022 23:14:06 +0100 Subject: [PATCH 06/13] Fixed an error about the DB creation fix --- program_database.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/program_database.py b/program_database.py index 20630a9..60672e1 100644 --- a/program_database.py +++ b/program_database.py @@ -24,13 +24,11 @@ def load_data(customer_database): result = mycursor.fetchall() print(result) for x in result: - if db_name == x[0]: - # mycursor.execute('DROP DATABASE ' + db_name) # delete old database - # mydb.commit() # make the changes official + if x[0] == 'customersdatabase1': print("The database already exists!") return None - mycursor.execute("CREATE DATABASE IF NOT EXISTS" + db_name) + mycursor.execute("CREATE DATABASE CustomersDatabase1") print("Database is created") except Error as e: print("Error while connecting to MySQL", e) From e09634575280555c58d08981a18cd1e8e9d5c69a Mon Sep 17 00:00:00 2001 From: tommasoagudio Date: Sun, 4 Dec 2022 23:29:58 +0100 Subject: [PATCH 07/13] fixed app not working in CMD fix --- program_database.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/program_database.py b/program_database.py index 60672e1..9ce1ca2 100644 --- a/program_database.py +++ b/program_database.py @@ -1,7 +1,7 @@ def load_data(customer_database): import pandas as pd - data = pd.read_csv("customer_segmentation.csv") + data = pd.read_csv("C:/Users/Tommy/Documents/GitHub/DatabaseProject/customer_segmentation.csv") import mysql.connector as mysql from mysql.connector import Error @@ -319,7 +319,7 @@ def query6(): # number of orders for a specific city and a specific payment_typ from mysql.connector import Error import pandas as pd - data = pd.read_csv("customer_segmentation.csv") + data = pd.read_csv("C:/Users/Tommy/Documents/GitHub/DatabaseProject/customer_segmentation.csv") possible_cities = [] possible_payment_types = [] for city in data["customer_city"]: @@ -361,7 +361,7 @@ def query7(): # return the avarage payment value for a specific payment type from mysql.connector import Error import pandas as pd - data = pd.read_csv("customer_segmentation.csv") + data = pd.read_csv("C:/Users/Tommy/Documents/GitHub/DatabaseProject/customer_segmentation.csv") possible_payment_types = [] for payments in data["payment_type"]: if payments not in possible_payment_types: @@ -395,7 +395,7 @@ def query8(): from mysql.connector import Error import pandas as pd - data = pd.read_csv("customer_segmentation.csv") + data = pd.read_csv("C:/Users/Tommy/Documents/GitHub/DatabaseProject/customer_segmentation.csv") possible_cities = [] for city in data["customer_city"]: if city not in possible_cities: From e2fc720902e2fae9874fc5486c1e1398a347c52f Mon Sep 17 00:00:00 2001 From: tommasoagudio <94019398+tommasoagudio@users.noreply.github.com> Date: Mon, 5 Dec 2022 15:25:16 +0100 Subject: [PATCH 08/13] release --- Database_ (1).ipynb | 596 -------------------------------------------- Database_.ipynb | 455 --------------------------------- program_database.py | 87 +++---- 3 files changed, 45 insertions(+), 1093 deletions(-) delete mode 100644 Database_ (1).ipynb delete mode 100644 Database_.ipynb diff --git a/Database_ (1).ipynb b/Database_ (1).ipynb deleted file mode 100644 index 3c7512d..0000000 --- a/Database_ (1).ipynb +++ /dev/null @@ -1,596 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
order_idcustomer_idorder_statusorder_purchase_timestamporder_approved_atorder_delivered_carrier_dateorder_delivered_customer_dateorder_estimated_delivery_datepayment_typepayment_installments...seller_idshipping_limit_datepricefreight_valueseller_cityseller_stateproduct_category_nameproduct_name_lenghtproduct_description_lenghtproduct_category_name_english
053cdb2fc8bc7dce0b6741e2150273451b0830fb4747a6c6d20dea0b8c802d7efdelivered2018-07-24 20:41:372018-07-26 03:24:272018-07-26 14:31:002018-08-07 15:27:452018-08-13 00:00:00boleto1...289cdb325fb7e7f891c38608bf9e09622018-07-30 03:24:27118.722.76belo horizonteSPperfumaria29178perfumery
186674ccaee19790309333210917b2c7d1b338293f35549b5e480b9a3d7bbf3cddelivered2018-08-09 11:37:352018-08-09 14:35:192018-08-10 14:34:002018-08-14 18:51:472018-08-22 00:00:00credit_card5...289cdb325fb7e7f891c38608bf9e09622018-08-13 14:31:29116.918.92belo horizonteSPperfumaria29178perfumery
2aee682982e18eb4714ce9f97b15af5e28858442ea4d5dc5bb9e118e8f728095ddelivered2018-07-09 18:46:282018-07-11 03:45:452018-07-11 15:01:002018-07-12 18:14:352018-07-18 00:00:00boleto1...289cdb325fb7e7f891c38608bf9e09622018-07-13 03:45:45118.79.34belo horizonteSPperfumaria29178perfumery
3d543201a9b42a1402ff97e65b439a48b971bf8f42a9f8cb3ead257854905b454delivered2018-08-21 10:00:252018-08-21 10:50:542018-08-22 15:21:002018-08-28 18:58:222018-09-10 00:00:00credit_card2...289cdb325fb7e7f891c38608bf9e09622018-08-23 10:50:54116.922.75belo horizonteSPperfumaria29178perfumery
4d543201a9b42a1402ff97e65b439a48b971bf8f42a9f8cb3ead257854905b454delivered2018-08-21 10:00:252018-08-21 10:50:542018-08-22 15:21:002018-08-28 18:58:222018-09-10 00:00:00credit_card2...289cdb325fb7e7f891c38608bf9e09622018-08-23 10:50:54116.922.75belo horizonteSPperfumaria29178perfumery
\n", - "

5 rows × 26 columns

\n", - "
" - ], - "text/plain": [ - " order_id customer_id \\\n", - "0 53cdb2fc8bc7dce0b6741e2150273451 b0830fb4747a6c6d20dea0b8c802d7ef \n", - "1 86674ccaee19790309333210917b2c7d 1b338293f35549b5e480b9a3d7bbf3cd \n", - "2 aee682982e18eb4714ce9f97b15af5e2 8858442ea4d5dc5bb9e118e8f728095d \n", - "3 d543201a9b42a1402ff97e65b439a48b 971bf8f42a9f8cb3ead257854905b454 \n", - "4 d543201a9b42a1402ff97e65b439a48b 971bf8f42a9f8cb3ead257854905b454 \n", - "\n", - " order_status order_purchase_timestamp order_approved_at \\\n", - "0 delivered 2018-07-24 20:41:37 2018-07-26 03:24:27 \n", - "1 delivered 2018-08-09 11:37:35 2018-08-09 14:35:19 \n", - "2 delivered 2018-07-09 18:46:28 2018-07-11 03:45:45 \n", - "3 delivered 2018-08-21 10:00:25 2018-08-21 10:50:54 \n", - "4 delivered 2018-08-21 10:00:25 2018-08-21 10:50:54 \n", - "\n", - " order_delivered_carrier_date order_delivered_customer_date \\\n", - "0 2018-07-26 14:31:00 2018-08-07 15:27:45 \n", - "1 2018-08-10 14:34:00 2018-08-14 18:51:47 \n", - "2 2018-07-11 15:01:00 2018-07-12 18:14:35 \n", - "3 2018-08-22 15:21:00 2018-08-28 18:58:22 \n", - "4 2018-08-22 15:21:00 2018-08-28 18:58:22 \n", - "\n", - " order_estimated_delivery_date payment_type payment_installments ... \\\n", - "0 2018-08-13 00:00:00 boleto 1 ... \n", - "1 2018-08-22 00:00:00 credit_card 5 ... \n", - "2 2018-07-18 00:00:00 boleto 1 ... \n", - "3 2018-09-10 00:00:00 credit_card 2 ... \n", - "4 2018-09-10 00:00:00 credit_card 2 ... \n", - "\n", - " seller_id shipping_limit_date price freight_value \\\n", - "0 289cdb325fb7e7f891c38608bf9e0962 2018-07-30 03:24:27 118.7 22.76 \n", - "1 289cdb325fb7e7f891c38608bf9e0962 2018-08-13 14:31:29 116.9 18.92 \n", - "2 289cdb325fb7e7f891c38608bf9e0962 2018-07-13 03:45:45 118.7 9.34 \n", - "3 289cdb325fb7e7f891c38608bf9e0962 2018-08-23 10:50:54 116.9 22.75 \n", - "4 289cdb325fb7e7f891c38608bf9e0962 2018-08-23 10:50:54 116.9 22.75 \n", - "\n", - " seller_city seller_state product_category_name product_name_lenght \\\n", - "0 belo horizonte SP perfumaria 29 \n", - "1 belo horizonte SP perfumaria 29 \n", - "2 belo horizonte SP perfumaria 29 \n", - "3 belo horizonte SP perfumaria 29 \n", - "4 belo horizonte SP perfumaria 29 \n", - "\n", - " product_description_lenght product_category_name_english \n", - "0 178 perfumery \n", - "1 178 perfumery \n", - "2 178 perfumery \n", - "3 178 perfumery \n", - "4 178 perfumery \n", - "\n", - "[5 rows x 26 columns]" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import pandas as pd\n", - "data = pd.read_csv('customer_segmentation.csv')\n", - "data.head()" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('customersdatabase',), ('customersdatabase2',), ('goshare',), ('information_schema',), ('mysql',), ('performance_schema',), ('sakila',), ('sys',), ('world',)]\n", - "Database is created\n" - ] - } - ], - "source": [ - "import mysql.connector as mysql\n", - "from mysql.connector import Error \n", - "\n", - "db_name='CustomersDatabaseFinal'\n", - "try:\n", - " mydb = mysql.connect(host='localhost', user='Alessandro', password='Terracina1!', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26)\n", - " if mydb.is_connected():\n", - " mycursor = mydb.cursor()\n", - " mycursor.execute('SHOW DATABASES')\n", - " result = mycursor.fetchall()\n", - " print(result)\n", - " for x in result:\n", - " if db_name == x[0]:\n", - " mycursor.execute('DROP DATABASE ' + db_name) # delete old database\n", - " mydb.commit() # make the changes official\n", - " print(\"The database already exists! The old database has been deleted!)\")\n", - " \n", - " mycursor.execute(\"CREATE DATABASE \"+ db_name)\n", - " print(\"Database is created\")\n", - "except Error as e:\n", - " print(\"Error while connecting to MySQL\", e)\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "141.46" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dataset = pd.read_csv('customer_segmentation.csv')\n", - "dataset.head()\n", - "dataset['payment_value'][0]" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\"USE CustomersDatabaseFinal\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " ''' \n", - " CREATE TABLE customer(\n", - " customer_unique_id VARCHAR(40),\n", - " customer_id VARCHAR(40),\n", - " customer_city VARCHAR(40),\n", - " customer_state VARCHAR(40),\n", - " PRIMARY KEY (customer_unique_id)\n", - " );\n", - " '''\n", - " )" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE seller(\n", - " seller_id VARCHAR(40),\n", - " seller_city VARCHAR(40),\n", - " seller_state VARCHAR(40),\n", - " PRIMARY KEY (seller_id)\n", - " );\n", - "\n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE product(\n", - " product_id VARCHAR(40),\n", - " price int,\n", - " freight_value int,\n", - " product_category_name VARCHAR(60),\n", - " product_description_lenght int,\n", - " PRIMARY KEY (product_id)\n", - " );\n", - "\n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "\n", - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE order_(\n", - " order_id VARCHAR(40),\n", - " order_status VARCHAR(40),\n", - " order_purchase_time DATETIME,\n", - " order_approved_at DATETIME,\n", - " order_delivered_carrier_date DATETIME,\n", - " order_delivered_customer_date DATETIME,\n", - " order_estimated_delivery_date DATETIME,\n", - " payment_type VARCHAR(40),\n", - " payment_value int,\n", - " payment_installments int,\n", - " order_customer VARCHAR(40),\n", - " order_product VARCHAR(40),\n", - " order_seller VARCHAR(40),\n", - " PRIMARY KEY (order_id),\n", - " FOREIGN KEY (order_customer) REFERENCES customer(customer_unique_id),\n", - " FOREIGN KEY (order_seller) REFERENCES seller(seller_id),\n", - " FOREIGN KEY (order_product) REFERENCES product(product_id)\n", - " ); \n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE offer(\n", - " id VARCHAR(40),\n", - " seller_id VARCHAR(40),\n", - " product_id VARCHAR(40),\n", - " PRIMARY KEY (id),\n", - " FOREIGN KEY (seller_id) REFERENCES seller(seller_id),\n", - " FOREIGN KEY (product_id) REFERENCES product(product_id)\n", - " ); \n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [], - "source": [ - "for i,row in data.iterrows():\n", - " sql = \"INSERT IGNORE INTO CustomersDatabaseFinal.customer VALUES (%s,%s,%s,%s)\"\n", - " mycursor.execute(sql, tuple([row['customer_unique_id'], row['customer_id'], row['customer_city'], row['customer_state']]))\n", - " #print(\"Record inserted\")\n", - " mydb.commit()" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": {}, - "outputs": [], - "source": [ - "for i,row in data.iterrows():\n", - " sql = \"INSERT IGNORE INTO CustomersDatabaseFinal.seller VALUES (%s,%s,%s)\"\n", - " mycursor.execute(sql, tuple([row['seller_id'], row['seller_city'], row['seller_state']]))\n", - " #print(\"Record inserted\")\n", - " mydb.commit()\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "df2 = data.loc[:,['product_id','seller_id']]\n", - "df2.drop_duplicates(inplace = True)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\"ALTER TABLE product DROP COLUMN product_seller\")" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": {}, - "outputs": [], - "source": [ - "for i,row in data.iterrows():\n", - " sql = \"INSERT IGNORE INTO CustomersDatabaseFinal.product VALUES (%s,%s,%s,%s,%s)\"\n", - " mycursor.execute(sql, tuple([row['product_id'], row['price'], row['freight_value'], row['product_category_name'], row['product_description_lenght']]))\n", - " #print(\"Record inserted\")\n", - " mydb.commit()\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "for i,row in data.iterrows():\n", - " sql = \"INSERT IGNORE INTO CustomersDatabaseFinal.order_ VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)\"\n", - " mycursor.execute(sql, tuple([row['order_id'], row['order_status'], row['order_purchase_timestamp'], row['order_approved_at'], row['order_delivered_carrier_date'], row['order_delivered_customer_date'], row['order_estimated_delivery_date'], row['payment_type'], row['payment_value'], row['payment_installments'], row['customer_unique_id'], row['product_id'] , row['seller_id']]))\n", - " #print(\"Record inserted\")\n", - " mydb.commit()" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": {}, - "outputs": [], - "source": [ - "c = 0\n", - "for i,row in df2.iterrows():\n", - " sql = \"INSERT IGNORE INTO CustomersDatabaseFinal.offer VALUES (%s,%s,%s)\"\n", - " mycursor.execute(sql, tuple([c, row['seller_id'], row['product_id']]))\n", - " c+=1\n", - " #print(\"Record inserted\")\n", - " mydb.commit()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "ename": "ProgrammingError", - "evalue": "1146 (42S02): Table 'customersdatabase.offers' doesn't exist", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mMySQLInterfaceError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\Tommy\\anaconda3\\lib\\site-packages\\mysql\\connector\\connection_cext.py\u001b[0m in \u001b[0;36mcmd_query\u001b[1;34m(self, query, raw, buffered, raw_as_string)\u001b[0m\n\u001b[0;32m 564\u001b[0m \u001b[0mquery\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mquery\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"utf-8\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 565\u001b[1;33m self._cmysql.query(\n\u001b[0m\u001b[0;32m 566\u001b[0m \u001b[0mquery\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mMySQLInterfaceError\u001b[0m: Table 'customersdatabase.offers' doesn't exist", - "\nThe above exception was the direct cause of the following exception:\n", - "\u001b[1;31mProgrammingError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_10964\\3945750071.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmycursor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"SELECT * FROM OFFERS\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;32mc:\\Users\\Tommy\\anaconda3\\lib\\site-packages\\mysql\\connector\\cursor_cext.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, operation, params, multi)\u001b[0m\n\u001b[0;32m 277\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 278\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 279\u001b[1;33m result = self._cnx.cmd_query(\n\u001b[0m\u001b[0;32m 280\u001b[0m \u001b[0mstmt\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 281\u001b[0m \u001b[0mraw\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_raw\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32mc:\\Users\\Tommy\\anaconda3\\lib\\site-packages\\mysql\\connector\\connection_cext.py\u001b[0m in \u001b[0;36mcmd_query\u001b[1;34m(self, query, raw, buffered, raw_as_string)\u001b[0m\n\u001b[0;32m 571\u001b[0m )\n\u001b[0;32m 572\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mMySQLInterfaceError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 573\u001b[1;33m raise get_mysql_exception(\n\u001b[0m\u001b[0;32m 574\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merrno\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmsg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msqlstate\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msqlstate\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 575\u001b[0m ) from err\n", - "\u001b[1;31mProgrammingError\u001b[0m: 1146 (42S02): Table 'customersdatabase.offers' doesn't exist" - ] - } - ], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3.10.6 ('spyder-env')", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.6" - }, - "orig_nbformat": 4, - "vscode": { - "interpreter": { - "hash": "56eeda6514f840d397735a62262def7a88f09e03751507b2b592ff0725e11aa6" - } - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/Database_.ipynb b/Database_.ipynb deleted file mode 100644 index 64a034c..0000000 --- a/Database_.ipynb +++ /dev/null @@ -1,455 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
order_idcustomer_idorder_statusorder_purchase_timestamporder_approved_atorder_delivered_carrier_dateorder_delivered_customer_dateorder_estimated_delivery_datepayment_typepayment_installments...seller_idshipping_limit_datepricefreight_valueseller_cityseller_stateproduct_category_nameproduct_name_lenghtproduct_description_lenghtproduct_category_name_english
053cdb2fc8bc7dce0b6741e2150273451b0830fb4747a6c6d20dea0b8c802d7efdelivered2018-07-24 20:41:372018-07-26 03:24:272018-07-26 14:31:002018-08-07 15:27:452018-08-13 00:00:00boleto1...289cdb325fb7e7f891c38608bf9e09622018-07-30 03:24:27118.722.76belo horizonteSPperfumaria29178perfumery
186674ccaee19790309333210917b2c7d1b338293f35549b5e480b9a3d7bbf3cddelivered2018-08-09 11:37:352018-08-09 14:35:192018-08-10 14:34:002018-08-14 18:51:472018-08-22 00:00:00credit_card5...289cdb325fb7e7f891c38608bf9e09622018-08-13 14:31:29116.918.92belo horizonteSPperfumaria29178perfumery
2aee682982e18eb4714ce9f97b15af5e28858442ea4d5dc5bb9e118e8f728095ddelivered2018-07-09 18:46:282018-07-11 03:45:452018-07-11 15:01:002018-07-12 18:14:352018-07-18 00:00:00boleto1...289cdb325fb7e7f891c38608bf9e09622018-07-13 03:45:45118.79.34belo horizonteSPperfumaria29178perfumery
3d543201a9b42a1402ff97e65b439a48b971bf8f42a9f8cb3ead257854905b454delivered2018-08-21 10:00:252018-08-21 10:50:542018-08-22 15:21:002018-08-28 18:58:222018-09-10 00:00:00credit_card2...289cdb325fb7e7f891c38608bf9e09622018-08-23 10:50:54116.922.75belo horizonteSPperfumaria29178perfumery
4d543201a9b42a1402ff97e65b439a48b971bf8f42a9f8cb3ead257854905b454delivered2018-08-21 10:00:252018-08-21 10:50:542018-08-22 15:21:002018-08-28 18:58:222018-09-10 00:00:00credit_card2...289cdb325fb7e7f891c38608bf9e09622018-08-23 10:50:54116.922.75belo horizonteSPperfumaria29178perfumery
\n", - "

5 rows × 26 columns

\n", - "
" - ], - "text/plain": [ - " order_id customer_id \\\n", - "0 53cdb2fc8bc7dce0b6741e2150273451 b0830fb4747a6c6d20dea0b8c802d7ef \n", - "1 86674ccaee19790309333210917b2c7d 1b338293f35549b5e480b9a3d7bbf3cd \n", - "2 aee682982e18eb4714ce9f97b15af5e2 8858442ea4d5dc5bb9e118e8f728095d \n", - "3 d543201a9b42a1402ff97e65b439a48b 971bf8f42a9f8cb3ead257854905b454 \n", - "4 d543201a9b42a1402ff97e65b439a48b 971bf8f42a9f8cb3ead257854905b454 \n", - "\n", - " order_status order_purchase_timestamp order_approved_at \\\n", - "0 delivered 2018-07-24 20:41:37 2018-07-26 03:24:27 \n", - "1 delivered 2018-08-09 11:37:35 2018-08-09 14:35:19 \n", - "2 delivered 2018-07-09 18:46:28 2018-07-11 03:45:45 \n", - "3 delivered 2018-08-21 10:00:25 2018-08-21 10:50:54 \n", - "4 delivered 2018-08-21 10:00:25 2018-08-21 10:50:54 \n", - "\n", - " order_delivered_carrier_date order_delivered_customer_date \\\n", - "0 2018-07-26 14:31:00 2018-08-07 15:27:45 \n", - "1 2018-08-10 14:34:00 2018-08-14 18:51:47 \n", - "2 2018-07-11 15:01:00 2018-07-12 18:14:35 \n", - "3 2018-08-22 15:21:00 2018-08-28 18:58:22 \n", - "4 2018-08-22 15:21:00 2018-08-28 18:58:22 \n", - "\n", - " order_estimated_delivery_date payment_type payment_installments ... \\\n", - "0 2018-08-13 00:00:00 boleto 1 ... \n", - "1 2018-08-22 00:00:00 credit_card 5 ... \n", - "2 2018-07-18 00:00:00 boleto 1 ... \n", - "3 2018-09-10 00:00:00 credit_card 2 ... \n", - "4 2018-09-10 00:00:00 credit_card 2 ... \n", - "\n", - " seller_id shipping_limit_date price freight_value \\\n", - "0 289cdb325fb7e7f891c38608bf9e0962 2018-07-30 03:24:27 118.7 22.76 \n", - "1 289cdb325fb7e7f891c38608bf9e0962 2018-08-13 14:31:29 116.9 18.92 \n", - "2 289cdb325fb7e7f891c38608bf9e0962 2018-07-13 03:45:45 118.7 9.34 \n", - "3 289cdb325fb7e7f891c38608bf9e0962 2018-08-23 10:50:54 116.9 22.75 \n", - "4 289cdb325fb7e7f891c38608bf9e0962 2018-08-23 10:50:54 116.9 22.75 \n", - "\n", - " seller_city seller_state product_category_name product_name_lenght \\\n", - "0 belo horizonte SP perfumaria 29 \n", - "1 belo horizonte SP perfumaria 29 \n", - "2 belo horizonte SP perfumaria 29 \n", - "3 belo horizonte SP perfumaria 29 \n", - "4 belo horizonte SP perfumaria 29 \n", - "\n", - " product_description_lenght product_category_name_english \n", - "0 178 perfumery \n", - "1 178 perfumery \n", - "2 178 perfumery \n", - "3 178 perfumery \n", - "4 178 perfumery \n", - "\n", - "[5 rows x 26 columns]" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import pandas as pd\n", - "data = pd.read_csv('customer_segmentation.csv')\n", - "data.head()" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('CustomersDatabase',), ('database_',), ('information_schema',), ('mockmidterm',), ('mysql',), ('performance_schema',), ('sys',)]\n", - "The database already exists! The old database has been deleted!)\n", - "Database is created\n" - ] - } - ], - "source": [ - "import mysql.connector as mysql\n", - "from mysql.connector import Error \n", - "\n", - "db_name='CustomersDatabase'\n", - "try:\n", - " mydb = mysql.connect(host='localhost', user='root', password='password123', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26)\n", - " if mydb.is_connected():\n", - " mycursor = mydb.cursor()\n", - " mycursor.execute('SHOW DATABASES')\n", - " result = mycursor.fetchall()\n", - " print(result)\n", - " for x in result:\n", - " if db_name == x[0]:\n", - " mycursor.execute('DROP DATABASE ' + db_name) # delete old database\n", - " mydb.commit() # make the changes official\n", - " print(\"The database already exists! The old database has been deleted!)\")\n", - " \n", - " mycursor.execute(\"CREATE DATABASE \"+ db_name)\n", - " print(\"Database is created\")\n", - "except Error as e:\n", - " print(\"Error while connecting to MySQL\", e)\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "141.46" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dataset = pd.read_csv('customer_segmentation.csv')\n", - "dataset.head()\n", - "dataset['payment_value'][0]" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\"USE customersdatabase\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " ''' \n", - " CREATE TABLE customer(\n", - " customer_unique_id VARCHAR(40),\n", - " customer_id VARCHAR(40),\n", - " customer_city VARCHAR(40),\n", - " customer_state VARCHAR(40),\n", - " PRIMARY KEY (customer_unique_id)\n", - " );\n", - " '''\n", - " )" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE seller(\n", - " seller_id VARCHAR(40),\n", - " seller_city VARCHAR(40),\n", - " seller_state VARCHAR(40),\n", - " PRIMARY KEY (seller_id)\n", - " );\n", - "\n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE product(\n", - " product_id VARCHAR(40),\n", - " price int,\n", - " freight_value int,\n", - " product_category_name VARCHAR(60),\n", - " product_description_lenght int,\n", - " product_seller VARCHAR(40),\n", - " PRIMARY KEY (product_id),\n", - " FOREIGN KEY (product_seller) REFERENCES seller(seller_id)\n", - " );\n", - "\n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "\n", - "\n", - "\n", - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE order_(\n", - " order_id VARCHAR(40),\n", - " order_status VARCHAR(40),\n", - " order_purchase_time TIMESTAMP,\n", - " order_approved_at TIMESTAMP,\n", - " order_delivered_carrier_date TIMESTAMP,\n", - " order_delivered_customer_date TIMESTAMP,\n", - " order_estimated_delivery_date TIMESTAMP,\n", - " payment_type VARCHAR(40),\n", - " payment_value int,\n", - " payment_installments int,\n", - " order_customer VARCHAR(40),\n", - " order_product VARCHAR(40),\n", - " order_seller VARCHAR(40),\n", - " PRIMARY KEY (order_id),\n", - " FOREIGN KEY (order_customer) REFERENCES customer(customer_unique_id),\n", - " FOREIGN KEY (order_seller) REFERENCES seller(seller_id),\n", - " FOREIGN KEY (order_product) REFERENCES product(product_id)\n", - " ); \n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3.9.13 ('base')", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "orig_nbformat": 4, - "vscode": { - "interpreter": { - "hash": "2a92ed693cb77f20c14c67822919cbb2c31a455d141e9ef517a60f96c81b2b70" - } - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/program_database.py b/program_database.py index 9ce1ca2..7580b84 100644 --- a/program_database.py +++ b/program_database.py @@ -1,7 +1,8 @@ def load_data(customer_database): import pandas as pd - - data = pd.read_csv("C:/Users/Tommy/Documents/GitHub/DatabaseProject/customer_segmentation.csv") + global file_path + file_path = input('Please insert the file path for the dataset: ') + data = pd.read_csv(file_path+"/customer_segmentation.csv") import mysql.connector as mysql from mysql.connector import Error @@ -17,7 +18,7 @@ def load_data(customer_database): user=username, password=password, auth_plugin="mysql_native_password", - ) # you can add the auth_plugin here too (ref line 26) + ) if mydb.is_connected(): mycursor = mydb.cursor() mycursor.execute("SHOW DATABASES") @@ -124,14 +125,14 @@ def load_data(customer_database): ] ), ) - # print("Record inserted") + mydb.commit() for i, row in data.iterrows(): sql = "INSERT IGNORE INTO CustomersDatabase1.seller VALUES (%s,%s,%s)" mycursor.execute( sql, tuple([row["seller_id"], row["seller_city"], row["seller_state"]]) ) - # print("Record inserted") + mydb.commit() for i, row in data.iterrows(): sql = "INSERT IGNORE INTO CustomersDatabase1.product VALUES (%s,%s,%s,%s,%s)" @@ -142,12 +143,12 @@ def load_data(customer_database): row["product_id"], row["price"], row["freight_value"], - row["product_category_name"], + row["product_category_name_english"], row["product_description_lenght"], ] ), ) - # print("Record inserted") + mydb.commit() for i, row in data.iterrows(): @@ -172,7 +173,7 @@ def load_data(customer_database): ] ), ) - # print("Record inserted") + mydb.commit() df2 = data.loc[:, ["product_id", "seller_id"]] df2.drop_duplicates(inplace=True) @@ -181,13 +182,13 @@ def load_data(customer_database): sql = "INSERT IGNORE INTO CustomersDatabase1.offer VALUES (%s,%s,%s)" mycursor.execute(sql, tuple([c, row["seller_id"], row["product_id"]])) c += 1 - # print("Record inserted") + mydb.commit() print("I loaded the dataset and built the database!\n") -def query1(): # returns the number of orders for each product +def query1(): import mysql.connector as mysql from mysql.connector import Error @@ -196,7 +197,7 @@ def query1(): # returns the number of orders for each product user=username, password=password, auth_plugin="mysql_native_password", - ) # you can add the auth_plugin here too (ref line 26) + ) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") sql = mycursor.execute( @@ -213,7 +214,7 @@ def query1(): # returns the number of orders for each product print(element) -def query2(): # returns the number of orders for each city +def query2(): import mysql.connector as mysql from mysql.connector import Error @@ -222,7 +223,7 @@ def query2(): # returns the number of orders for each city user=username, password=password, auth_plugin="mysql_native_password", - ) # you can add the auth_plugin here too (ref line 26) + ) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") sql = mycursor.execute( @@ -239,7 +240,7 @@ def query2(): # returns the number of orders for each city print(element) -def query3(): # returns the number of orders for each customer +def query3(): import mysql.connector as mysql from mysql.connector import Error @@ -248,7 +249,7 @@ def query3(): # returns the number of orders for each customer user=username, password=password, auth_plugin="mysql_native_password", - ) # you can add the auth_plugin here too (ref line 26) + ) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") sql = mycursor.execute( @@ -265,7 +266,7 @@ def query3(): # returns the number of orders for each customer print(element) -def query4(): # returns the avarage number of installments +def query4(): import mysql.connector as mysql from mysql.connector import Error @@ -274,7 +275,7 @@ def query4(): # returns the avarage number of installments user=username, password=password, auth_plugin="mysql_native_password", - ) # you can add the auth_plugin here too (ref line 26) + ) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") sql = mycursor.execute( @@ -289,7 +290,7 @@ def query4(): # returns the avarage number of installments print(element) -def query5(): # returns the payment value, price and installments for each order. +def query5(): import mysql.connector as mysql from mysql.connector import Error @@ -298,7 +299,7 @@ def query5(): # returns the payment value, price and installments for each orde user=username, password=password, auth_plugin="mysql_native_password", - ) # you can add the auth_plugin here too (ref line 26) + ) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") sql = mycursor.execute( @@ -314,12 +315,12 @@ def query5(): # returns the payment value, price and installments for each orde print(element) -def query6(): # number of orders for a specific city and a specific payment_type +def query6(): import mysql.connector as mysql from mysql.connector import Error import pandas as pd - data = pd.read_csv("C:/Users/Tommy/Documents/GitHub/DatabaseProject/customer_segmentation.csv") + data = pd.read_csv(file_path+"/customer_segmentation.csv") possible_cities = [] possible_payment_types = [] for city in data["customer_city"]: @@ -336,7 +337,7 @@ def query6(): # number of orders for a specific city and a specific payment_typ user=username, password=password, auth_plugin="mysql_native_password", - ) # you can add the auth_plugin here too (ref line 26) + ) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") @@ -348,68 +349,67 @@ def query6(): # number of orders for a specific city and a specific payment_typ where c.customer_unique_id = o.order_customer and c.customer_city = %s and o.payment_type = %s; """, - (city, payment_type), + (city, payment_type), ) mycursor.execute(sql) result = mycursor.fetchall() for element in result: print(element) - -def query7(): # return the avarage payment value for a specific payment type + +def query7(): import mysql.connector as mysql from mysql.connector import Error import pandas as pd - data = pd.read_csv("C:/Users/Tommy/Documents/GitHub/DatabaseProject/customer_segmentation.csv") - possible_payment_types = [] - for payments in data["payment_type"]: - if payments not in possible_payment_types: - possible_payment_types.append(payments) - payment_type = list(input("please enter the desired payment type ")) - print("Possible payment types to select: ", possible_payment_types) + data = pd.read_csv(file_path+"/customer_segmentation.csv") + possible_types = [] + for types in data["payment_type"]: + if types not in possible_types: + possible_types.append(types) + print("Possible cities to select: ", possible_types) + payment_type = input("please enter the desired payment type: ") mydb = mysql.connect( host="localhost", user=username, password=password, auth_plugin="mysql_native_password", - ) # you can add the auth_plugin here too (ref line 26) + ) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") sql = mycursor.execute( """ - select round(avg(o.payment_value)), o.payment_type from order_ as o - where o.payment_type = %s; + select avg(o.payment_value), o.payment_type from order_ as o + where o.payment_type = %s; """, - (payment_type), + (payment_type,) ) mycursor.execute(sql) result = mycursor.fetchall() for element in result: print(element) - def query8(): import mysql.connector as mysql from mysql.connector import Error import pandas as pd - data = pd.read_csv("C:/Users/Tommy/Documents/GitHub/DatabaseProject/customer_segmentation.csv") + data = pd.read_csv(file_path+"/customer_segmentation.csv") possible_cities = [] for city in data["customer_city"]: if city not in possible_cities: possible_cities.append(city) print("Possible cities to select: ", possible_cities) - amount = int(input("please enter minimum amount \n")) - city = input("please enter the desired city \n") + amount = int(input("please enter minimum amount: ")) + city = input("please enter the desired city: ") mydb = mysql.connect( host="localhost", user=username, password=password, auth_plugin="mysql_native_password", - ) # you can add the auth_plugin here too (ref line 26) + ) mycursor = mydb.cursor() mycursor.execute("USE CustomersDatabase1") sql = mycursor.execute( @@ -424,6 +424,9 @@ def query8(): ) mycursor.execute(sql) result = mycursor.fetchall() + if len(result) == 0: + print('There are no orders in this city with amount: ',amount) + return None for element in result: print(element) @@ -432,7 +435,7 @@ def query8(): print("Welcome to our project!\n") load_data("customer_segmentation.csv") - valid_choices = ["1", "2", "3", "4", "5", "6", "7", "8", "quit"] + valid_choices = [ "Query to show the number of orders for each product: ","1","Query to show the number of orders for each city: ", "2", "Query to show the number of orders for each customer: ","3","Query to show the avarage number of installments: ", "4","Query to show the avarage number of installments: ", "5","Query to show the number of orders for your desired city and payment type: ", "6","query will show the average payment value for the desired payment type: ", "7","Query to show the customers that have spent at least the desired amount and living in the determined city: ", "8", "quit"] while True: print("possible choices: \n") From d0096d8caba512fed9e15f449e46c82e992279c3 Mon Sep 17 00:00:00 2001 From: tommasoagudio <94019398+tommasoagudio@users.noreply.github.com> Date: Mon, 5 Dec 2022 16:04:46 +0100 Subject: [PATCH 09/13] kk --- program_database.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/program_database.py b/program_database.py index 7580b84..54b1d3f 100644 --- a/program_database.py +++ b/program_database.py @@ -254,10 +254,10 @@ def query3(): mycursor.execute("USE CustomersDatabase1") sql = mycursor.execute( """ - select count(c.customer_id),C.customer_id from customer as c, order_ as o - where c.customer_unique_id = o.order_customer - group by c.customer_id - order by count(c.customer_unique_id) DESC; + select count(s.seller_id),s.seller_id from seller as s, order_ as o + where s.seller_id = o.order_seller + group by s.seller_id + order by count(s.seller_id) DESC; """ ) mycursor.execute(sql) @@ -435,7 +435,7 @@ def query8(): print("Welcome to our project!\n") load_data("customer_segmentation.csv") - valid_choices = [ "Query to show the number of orders for each product: ","1","Query to show the number of orders for each city: ", "2", "Query to show the number of orders for each customer: ","3","Query to show the avarage number of installments: ", "4","Query to show the avarage number of installments: ", "5","Query to show the number of orders for your desired city and payment type: ", "6","query will show the average payment value for the desired payment type: ", "7","Query to show the customers that have spent at least the desired amount and living in the determined city: ", "8", "quit"] + valid_choices = [ "Query to show the number of orders for each product: ","1","Query to show the number of orders for each city: ", "2", "Query to show the number of orders sold by sellers: ","3","Query to show the avarage number of installments: ", "4","Query to show the avarage number of installments: ", "5","Query to show the number of orders for your desired city and payment type: ", "6","query will show the average payment value for the desired payment type: ", "7","Query to show the customers that have spent at least the desired amount and living in the determined city: ", "8", "quit"] while True: print("possible choices: \n") @@ -458,7 +458,7 @@ def query8(): query2() continue elif choice == "3": - print("This query will show the number of orders for each customer \n") + print("This query will show the number of orders sold by each seller \n") query3() continue elif choice == "4": From 93e629ef22e4981c9536a2cca09667ed66fb5f15 Mon Sep 17 00:00:00 2001 From: AlessandroNatoli Date: Mon, 5 Dec 2022 16:39:05 +0100 Subject: [PATCH 10/13] Update program_database.py commited plots --- program_database.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/program_database.py b/program_database.py index 54b1d3f..c008060 100644 --- a/program_database.py +++ b/program_database.py @@ -191,6 +191,8 @@ def load_data(customer_database): def query1(): import mysql.connector as mysql from mysql.connector import Error + import matplotlib.pyplot as plt + import pandas as pd mydb = mysql.connect( host="localhost", @@ -210,6 +212,13 @@ def query1(): ) mycursor.execute(sql) result = mycursor.fetchall() + df=pd.DataFrame(result) + df.columns=["Categories","Occurencies"] + print(df) + df.plot(x="Categories", y="Occurencies", kind='bar') + plt.xlabel("Categories") + plt.ylabel('Number of occurencies') + plt.title('Frequence of different Categories') for element in result: print(element) @@ -217,6 +226,8 @@ def query1(): def query2(): import mysql.connector as mysql from mysql.connector import Error + import matplotlib.pyplot as plt + import pandas as pd mydb = mysql.connect( host="localhost", @@ -236,6 +247,12 @@ def query2(): ) mycursor.execute(sql) result = mycursor.fetchall() + df=pd.DataFrame(result) + df.columns=["Occurencies","Cities"] + df.plot(x="Cities", y="Occurencies", kind='bar') + plt.xlabel("Cities") + plt.ylabel('Number of occurencies') + plt.title('Frequence of different Cities') for element in result: print(element) @@ -243,6 +260,8 @@ def query2(): def query3(): import mysql.connector as mysql from mysql.connector import Error + import matplotlib.pyplot as plt + import pandas as pd mydb = mysql.connect( host="localhost", @@ -262,6 +281,13 @@ def query3(): ) mycursor.execute(sql) result = mycursor.fetchall() + df=pd.DataFrame(result) + df.columns=["Occurencies","Cities"] + print(df) + df.plot(x="Cities" , kind='bar') + plt.xlabel("Cities") + plt.ylabel('Number of occurencies') + plt.title('Frequence of different Cities') for element in result: print(element) @@ -269,6 +295,8 @@ def query3(): def query4(): import mysql.connector as mysql from mysql.connector import Error + import matplotlib.pyplot as plt + import pandas as pd mydb = mysql.connect( host="localhost", @@ -293,6 +321,7 @@ def query4(): def query5(): import mysql.connector as mysql from mysql.connector import Error + import matplotlib.pyplot as plt mydb = mysql.connect( host="localhost", From e59d65e40c164bcd34b6e89a0fa13a85da310fd4 Mon Sep 17 00:00:00 2001 From: tommasoagudio <94019398+tommasoagudio@users.noreply.github.com> Date: Mon, 5 Dec 2022 17:11:48 +0100 Subject: [PATCH 11/13] changed some queries --- program_database.py | 50 ++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/program_database.py b/program_database.py index c008060..1e42973 100644 --- a/program_database.py +++ b/program_database.py @@ -282,12 +282,12 @@ def query3(): mycursor.execute(sql) result = mycursor.fetchall() df=pd.DataFrame(result) - df.columns=["Occurencies","Cities"] + df.columns=["Occurencies","Seller"] print(df) - df.plot(x="Cities" , kind='bar') - plt.xlabel("Cities") + df.plot(x="Seller" , kind='Occurencies') + plt.xlabel("Sellers") plt.ylabel('Number of occurencies') - plt.title('Frequence of different Cities') + plt.title('Frequency of different Sellers') for element in result: print(element) @@ -352,13 +352,15 @@ def query6(): data = pd.read_csv(file_path+"/customer_segmentation.csv") possible_cities = [] possible_payment_types = [] - for city in data["customer_city"]: - if city not in possible_cities: - possible_cities.append(city) + yes_no = input('Do you want to see the list of all possible cities? ').lower() + if yes_no == 'yes': + for city in data["customer_city"]: + if city not in possible_cities: + possible_cities.append(city) + print("Possible cities to select: ", possible_cities) for payments in data["payment_type"]: if payments not in possible_payment_types: possible_payment_types.append(payments) - print("Possible cities to select: ", possible_cities) print("") print("Possible payment types to select: ", possible_payment_types) mydb = mysql.connect( @@ -396,7 +398,7 @@ def query7(): for types in data["payment_type"]: if types not in possible_types: possible_types.append(types) - print("Possible cities to select: ", possible_types) + print("Possible payment types to select: ", possible_types) payment_type = input("please enter the desired payment type: ") mydb = mysql.connect( @@ -426,13 +428,21 @@ def query8(): data = pd.read_csv(file_path+"/customer_segmentation.csv") possible_cities = [] - for city in data["customer_city"]: - if city not in possible_cities: - possible_cities.append(city) - print("Possible cities to select: ", possible_cities) + yes_no = input('Do you want to see the list of all possible cities? ').lower() + if yes_no == 'yes': + for city in data["customer_city"]: + if city not in possible_cities: + possible_cities.append(city) + print("Possible cities to select: ", possible_cities) amount = int(input("please enter minimum amount: ")) city = input("please enter the desired city: ") + possible_types = [] + for types in data["payment_type"]: + if types not in possible_types: + possible_types.append(types) + print("Possible payment types to select: ", possible_types) + payment_type = input('please enter the desired payment type: ') mydb = mysql.connect( host="localhost", user=username, @@ -443,13 +453,11 @@ def query8(): mycursor.execute("USE CustomersDatabase1") sql = mycursor.execute( """ - select c.customer_id, sum(o.payment_value) as x, c.customer_city from customer as c, order_ as o - where c.customer_unique_id = o.order_customer and c.customer_city = %s - group by c.customer_id,o.payment_value, c.customer_city - having x > %s - order by o.payment_value DESC; + select count(c.customer_id), sum(o.payment_value) as x, c.customer_city, o.payment_type from customer as c, order_ as o + where c.customer_unique_id = o.order_customer and c.customer_city = %s and o.payment_type = %s + having x > %s; """, - (city, amount), + (city,payment_type, amount,), ) mycursor.execute(sql) result = mycursor.fetchall() @@ -464,7 +472,7 @@ def query8(): print("Welcome to our project!\n") load_data("customer_segmentation.csv") - valid_choices = [ "Query to show the number of orders for each product: ","1","Query to show the number of orders for each city: ", "2", "Query to show the number of orders sold by sellers: ","3","Query to show the avarage number of installments: ", "4","Query to show the avarage number of installments: ", "5","Query to show the number of orders for your desired city and payment type: ", "6","query will show the average payment value for the desired payment type: ", "7","Query to show the customers that have spent at least the desired amount and living in the determined city: ", "8", "quit"] + valid_choices = [ "Query to show the number of orders for each product: ","1","Query to show the number of orders for each city: ", "2", "Query to show the number of orders sold by sellers: ","3","Query to show the avarage number of installments: ", "4","Query to show the avarage number of installments: ", "5","Query to show the number of orders for your desired city and payment type: ", "6","query will show the average payment value for the desired payment type: ", "7","Query to show the number of customers that have spent at least the desired amount and living in the determined city: ", "8", "quit"] while True: print("possible choices: \n") @@ -514,7 +522,7 @@ def query8(): continue elif choice == "8": print( - "This query will show the customers that have spent at least the desired amount and living in the determined city \n" + "This query will show the number of customers that have spent at least the desired amount, are living in a determined city and have used a specific payment type \n" ) query8() continue From 4bb275e8f05e75b122dae26b26e40ede2e16272d Mon Sep 17 00:00:00 2001 From: AlessandroNatoli Date: Mon, 5 Dec 2022 17:19:13 +0100 Subject: [PATCH 12/13] minor details about app . --- program_database.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/program_database.py b/program_database.py index 1e42973..77469eb 100644 --- a/program_database.py +++ b/program_database.py @@ -384,6 +384,8 @@ def query6(): ) mycursor.execute(sql) result = mycursor.fetchall() + if len(result) == 0: + print('There are no results that satisty your input: ',city , amount) for element in result: print(element) @@ -418,6 +420,8 @@ def query7(): ) mycursor.execute(sql) result = mycursor.fetchall() + if len(result) == 0: + print('There is no payment type that you wanted: ',payment_type) for element in result: print(element) From 5977eef8ffd177d8a64061934b9137c1e4626df3 Mon Sep 17 00:00:00 2001 From: AlessandroNatoli Date: Mon, 5 Dec 2022 22:29:46 +0100 Subject: [PATCH 13/13] did the readme wrote the readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index b0a9d44..a91c36e 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ Database project repository + +When you run the application, you will encounter 3 initial inputs. +-First will ask you to insert your path to enter the folder, it should be of type: C:/pathnametofolder (you don't need to add the "/" at the end of the path, we have already done it for you) +-Then it will ask you for your username, that is the username you use to enter the localhost for mysql +-Lastly it will ask you for the password to enter the localhost + +This is done so you can easily execute the application without having to edit the code itself. After doing this initial authentification, you will enter the application, create the database and populate the tables automatically. When the process is finished you will recieve an output line stating that the database has been created and the data has been loaded into the tables of the database. Afterwards you will recieve the 8 queries that you can choose to run. +