Home > Learning, Lesson, Problem, Projects/Experiments, Python > Python: Cooking App P-3

Python: Cooking App P-3



Learning : Python, Data-Base, SQlite
Subject: Writing a Cooking Application

Over View: In last post (Part-2) we wrote the codes for Main-Menu and other sub-Menus, today in this post we will work on “Setting Menu”.
Setting Menu has three items:
1. Create the needed Tables. [To create all tables we need in the database]
2. Drop Tables. [If any things goes wrong then we can Drop (Delete) a Table (or more) ]
3. Insert Dummy Data. [To Insert ONE Record in the tables. (Record 0)]

First thing we need to create a database and set a connection to it, also we will import the [sqlite3 and os], to do so we will write this code:


  import sqlite3, os

  # Create the data-base & name it.
  db_conn = sqlite3.connect ("cooking.db")    
  
   # set the connection.
  c = db_conn.cursor() 


Now we need to write three Functions for the Setting Menu, we will start with Table creation code and here I am posting the Data-Base Tables Diagram ..

Click to Enlarge

 # Function to Create the Tables


def create_tables_() :    # to create tables.

    sql_recipes = "CREATE TABLE if not exists recipes (r_id INTEGER PRIMARY KEY AUTOINCREMENT, r_name text, r_date text, r_type text, r_details text, r_tags text )"

    sql_photo = "CREATE TABLE if not exists photo (p_id  PRIMARY KEY AUTOINCREMENT ,r_id integer , p_name text)"

    sql_ingredients_list = "CREATE TABLE if not exists ingredients_list (i_l_id PRIMARY KEY AUTOINCREMENT, i_name text )"

    sql_rec_ingredient ="CREATE TABLE if not exists rec_ingredient (ing_id INTEGER PRIMARY KEY AUTOINCREMENT, i_l_id integer, r_id integer)"

    c.execute(sql_recipes)
    db_conn.commit()

    c.execute(sql_photo)
    db_conn.commit()

    c.execute(sql_ingredients_list)
    db_conn.commit()

    c.execute(sql_rec_ingredient)
    db_conn.commit()

    input('\n   .. Cooking  Tables created..  Press any key .. ')



So now if we call this function “create_tables_() “, Tables will be created. Another function in the Setting Menu is Drop a Table, as we mention above we need this function in case we need to Delete a Table and create it again, in this function first we will list all the tables in the database and ask the user to Enter the Table need to be delete. Here is the code behind this function:

 # Drop a Table.

def drop_table():

    try:
        # First we will print-out the list of Tables.
        c.execute("SELECT name FROM sqlite_master  WHERE type = 'table'")
        db_tables =[]
        for each in c.fetchall() :
            for t in each :
                db_tables.append(t)
        print('\n  Table List .. ')
        for x in range (len(db_tables)) :
            print((x+1),db_tables[x])
        t = int(input('\n  Enter the number next to Table you want to Drop:  '))
        c.execute("DROP TABLE {}".format(db_tables[t-1]))
        db_conn.commit()
        print('\n ... One Table been Dropped')

    except:
        print('\n  No Tables with this name .. ')

    input('\n  . . . Press any key  . . ')



Last code to write in this post is to Insert One-Record in the tables, the reason of doing this manually (Hard-Coded) is that we have some “Primary key AUTOINCREMENT” fields in our tables, and to make it AUTOINCREMENT there MUST be a number first so we will enter the ZERO record (0).

 # Function to Insert the Zero Record

def insert_rec_0():

    c.execute ("INSERT INTO recipes (r_id) VALUES(:r_id)",{"r_id":0})
    
    c.execute ("INSERT INTO ingredients_list (i_l_id) VALUES(:i_l_id)",{"i_l_id":0})
    
    c.execute ("INSERT INTO rec_ingredient (ing_id) VALUES(:ing_id)",{"ing_id":0})
    
    c.execute ("INSERT INTO photo (p_id) VALUES(:p_id)",{"p_id":0})

    db_conn.commit()
   
    input('\n ...Dummy records been Inserted ....  Press any key  .. ')


In Next post we will write the codes for the functions in the “INGREDIENT MENU” to Add, Delete and Edit Ingredient List.



To Download my Python code (.py) files Click-Here




Follow me on Twitter..




By: Ali Radwani




  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: