Archive
Python: Coffee Consumption – P2
Learning : Python, SQlite3, Dataset, Pandas,
Subject: Create Coffee Consumption Application.
[NOTE: To keep the code as simple as we can, We WILL NOT ADD any user input Varevecations. Assuming that our user will Enter the right inputs.]
In this part (Part-2) of Coffee Consumption App, we will fill in some code into selected Functions. We will do the following:
- Calling the Main-Menu Function.
- Create header function.
- Create the DataBase.
- Create the Tables.
- Insert the Zero records.
- Insert data into Lookups Table.
So, let’s start with writing the main application body and calling the Main-Menu to trigger a Function based on the user selection. This section will be a calling the main_menu() in a while loop .. here is the code ..
# Main application body while True : user_select = main_menu() if user_select == '1' : new_data() if user_select == '2' : edit_record() if user_select == '3' : delete_record() if user_select == '4' : #delete_record() pass if user_select == '6' : c_name_type_size_manager() if user_select == '7' : create_data_base() if user_select == '99' : input('\n\n Thank you for using this Appliation. Press Enter.. > ') break
If we run the application now, we will see the main-menu but nothing will work because we still did not write any real Functions in the application.
Tables and Zero records:
Now we will write tha function to create the Tables and inserting the Zero record in each of them. All the codes will be in a function called: def create_data_base () : here are the name of the tables we will create:
coffee_con
coffee_name
coffee_type
coffee_size
and here is the full code in the function, in first part we will create the tables, second part will insert the zero record, last we will insert data in the lookup tables (coffee_name, coffee_type and coffee_size)
# def create_data_base () def create_data_base () : os.system('clear') line2 ="Create Data Base" header(line2,3,11) print('\n All the data in the Data-Base will be removed, and can''t be retrieved. ') if input('\n Press [Y] to Continue, anything else will Stop and Exite. > ') not in ['y','Y'] : input('\n You Select to stop and Exit. Press any Key .. > ') return # Part 1: CREATE TABLE: sql_coffee_con_t = "CREATE TABLE if not exists coffee_con (c_id INTEGER PRIMARY KEY AUTOINCREMENT, date_t,cn_id int, ct_id int, cs_id int, sex text, rank int )" sql_coffee_name_t = "CREATE TABLE if not exists coffee_name (cn_id INTEGER PRIMARY KEY AUTOINCREMENT, c_name text )" sql_coffee_type_t = "CREATE TABLE if not exists coffee_type (ct_id INTEGER PRIMARY KEY AUTOINCREMENT, c_type text )" sql_coffee_size_t = "CREATE TABLE if not exists coffee_size (cs_id INTEGER PRIMARY KEY AUTOINCREMENT, c_size text )" # Execute the commands c.execute(sql_coffee_con_t) db_conn.commit() c.execute(sql_coffee_name_t) db_conn.commit() c.execute(sql_coffee_type_t) db_conn.commit() c.execute(sql_coffee_size_t) db_conn.commit() # Part 2: Inserting ZERO records. c.execute ("INSERT INTO coffee_con (date_t , cn_id, ct_id, cs_id, sex, rank) VALUES(:date_t , :cn_id, :ct_id, :cs_id, :sex, :rank)",{'date_t':'0' ,'cn_id' :0,'ct_id' :0,'cs_id' :0,'sex' :'0','rank' :0}) db_conn.commit() c.execute ("INSERT INTO coffee_name (c_name) VALUES(:c_name )",{"c_name":'0'}) db_conn.commit() c.execute ("INSERT INTO coffee_type (c_type) VALUES(:c_type )",{"c_type":'0'}) db_conn.commit() c.execute ("INSERT INTO coffee_size (c_size) VALUES(:c_size )",{"c_size":'0'}) db_conn.commit() # Part 3: Inserting Basic Information coffee_Name_list = ['Black','Latte','Espresso','Americano','Cappuccino','Mocha', 'Lungo', 'Flat white', 'Irish', 'Macchiato','Ristretto','Iced coffee'] coffeeType_list = ['3n1' ,'Pods','Grounded'] coffeeSize_list = ['Samll', 'Medium', 'Large', 'Venti'] for each in coffeeName_list: c.execute ("INSERT INTO coffee_name (c_name) VALUES(:c_name )",{"c_name":each}) db_conn.commit() for each in coffeeType_list: c.execute ("INSERT INTO coffee_type (c_type) VALUES(:c_type )",{"c_type":each}) db_conn.commit() for each in coffeeSize_list: c.execute ("INSERT INTO coffee_size (c_size) VALUES(:c_size )",{"c_size":each}) db_conn.commit() input('\n\n Data-Base Created, Basic Information Inserted. press any key to continue. > ')
[ The Full Code is Available in Download Page. ]
Last Function in this part is the header, it’s just a Title that will bee displayed in a box at the top of each page. Here is the code ..
# Header of the Application def header(line2,b_l2,a_l2): os.system('clear') # The Project Name. line_1 = "Coffee Consumption" stars = 40 print('\n',' '*4,'*'*stars) print(' '*5,'***',' '*5,line_1,' '*7,'***') print(' '*5,'***',' '*(stars - 8),'***') print(' '*5,'***',' '*b_l2,line2,' '*a_l2,'***') print(' '*5,'***',' '*(stars - 8),'***') print(' '*5,'*'* stars,'\n')
What’s Coming: In Part-3 we will do the Follwing:
- Writing a function show_list.
- Writing the function to Add New Coffee Name.
- Writing the function to Edit a Coffee Name.
- Writing the function to Delete a Coffee Name.
..:: Have Fun with Coding ::.. 🙂
Part 1 | Part 2 | Part 3 | Part 4 | Part 5 |
Part 6 | Part – | Part – | Part – | Part – |
To Download my Python code (.py) files Click-Here
By: Ali Radwani