What’re you eating? Part 5
It’s been a while since I’ve written on this project, but it hasn’t been far from my mind. I’ve had great discussions with colleagues and friends about implementation options and improvement suggestions which have kept my mind active and helped me form out the next few stages of this project. Something that came up several times was what I’ll do if I decide something wasn’t the right choice - what if there’s a better way to do [x]? Or if I decide to approach [y] entirely differently? I’ll revisit it! This space isn’t meant to be just a step-by-step guide for setting up projects - if I wanted to do that then it’d just be directions and script blocks for people to copy/paste - it’s meant to be following through my thought process and decisions so everyone can benefit. With that out of the way - let’s begin!
Last time I had just got my recipes digitalised (although they still needed some TLC to clean up the data and make them look nice) and I said my next step was to create a database for my pantry items. For those of you who aren’t aware, a database is a collection of tables that are, in turn, made up of fields & data. After investigating several potential databases I settled on SQLite - no particular logic here, it just seemed to fulfil my needs on paper and I can always change it if I need to. Installing it on the pi and creating a database was incredibly easy which was a big help. In the terminal:
sudo apt install sqlite3 sqlite3 pantry.db
Now that I’ve got my pantry.db database, I had to think about what fields I’d need in my currentStock table. After pulling out and inspecting a bunch of items I had in the cupboard and considering the common factors and what I might need/want to use, I came up with the following list:
id INTEGER - essentially a row number
name TEXT - product name
size TEXT - how many units - separated out from size_type to reduce input variation
size_type TEXT - grams/kilos/millilitres/litres - I plan to have these entered via radio buttons
expiry_date TEXT - either explicit or estimated - I’ll probably look to have a calendar widget for this
expiry_type TEXT - best before or use by
barcode TEXT - potentially to scan items in the future to speed up entry
I set all of the product information to text types mostly for my own ease, but if I’m going to be interacting with the data with python then it won’t matter too much anyway. I can now create the first table in my database with the following command:
CREATE TABLE currentStock(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, size TEXT, size_type TEXT,expiry_date TEXT, expiry_type TEXT, barcode TEXT);
This command is pretty straight-forward if you understand a bit about the SQL language (if you don’t, it’s pretty easy to pick up the basics) but just to explain PRIMARY KEY and AUTOINCREMENT: Primary Key indicates that the field is a unique identifier for that row and autoincrement just means that I don’t have to enter the value (which would involve my swiss-cheese brain remembering what number I was up to).
Now I have to create an HTML form to send data to the table - the process is pretty straight-forward and well documented so I’ll skip most of the ‘working' out’ (read: fiddling) I did to get it looking like how I wanted. Behold: