Last time we did a few basic operations with SQLite in Raspberry Pi. We learned how to set up SQLite3, create the first database file and fill it with data. Using SQL commands, we were able to select data and print it on the terminal screen. But eventually, you will face more complex queries or store data automatically so other routines could read it. For instance, you will build a simple project that would read data from the digital temperature sensor. One of the great ways of storing data into a database is to use a python script. Anyway, you are probably going to use it for GPIO operations. So let us learn how to store simple data to SQLite database using python.
If you are doing some data logging, sensor reading, or another routine task with Raspberry Pi, you probably think of using the database. The list of database software choices is quite long, but you will end with a single or few tables in the database in most cases. The first thought might be MySQL – a well-known database server in WWW. Anyway, this is a pretty heavy tool to have running on Raspberry Pi. In my opinion, SQLite is probably the most suitable choice. Because it is serverless, lightweight, opensource, and supports most SQL code. Another handy thing is that SQLite stores data in a single file which can be stored anywhere.
Raspberry PI, by default, has only one memory available – an SD card that also holds the kernel itself. Unless you are using a large SD, eventually, you will run out of space -especially if you are dealing with media files or playing with the camera module. The easiest and cheapest way of expanding memory is to use a USB Flash drive. If you are accessing PI from the terminal screen, then probably one of the built-in USB ports is free, and you can attach a drive directly to it; otherwise, use USB HUB – better with an external power option.
Probably this would be unwise to go through a long list of available Unix commands. It is quite long, and there is no reason to point out each of them here. You can take a look at some basic ones in the following list. It is more important to learn how to use them and get the desired result by building more complex commands. Commands can also be combined into a single line using piping. In this case, the output of one command becomes the input of the next one, and so on. Let’s go with a few examples. We all know that Raspberry Pi comes with Python installed. So we should expect to find lots of .py files here: sudo find / -name *.py this throws us a large list of file names: Finding and displaying files that way is useless. Viewing is even more painful. Let’s say we simply want to count all .py files. For this, we use the same command, but instead of throwing the list to the terminal, we feed it to another command that does the counting of lines (we get every file in a new line):
Previously we looked at simple terminal commands like ls, cd. We know that ls output all files in the working directory. But in practice, command without options and parameters is almost useless. You may need to list special files or list files from the specified directory in more complex situations without going to it. This is why UNIX commands are run with options and parameters command [options] [parameters] There can be more than one option and parameter for a single command. For instance, we used a cd .. command to go to the previous directory. Dots .. are nothing more than parameters. Let’s see what parameters we have with the ls command. ls -l It displays a list of files in a long format. You can see that we get much more information by adding a simple option -l. Let’s say we want to display files in a long format from a different than a working directory. Then we need to type in the path to the directory as a parameter.
Computer users today are so attached to graphical interfaces. Sometimes it seems that people help computers to do the tasks. Can you imagine how many mouse clicks are required to do something simple? This is visible when the task is cyclic like “find, sort, delete.” Sometimes you find yourself just clicking the mouse and not seeing the result. What you would do half a day clicking the mouse can be performed with the single command line. The question is how to be that smart and feel like a fish in the water in front of the command prompt, shell, or bash – call it however you want. It is a system program that accepts typed commands from used and performs tasks. If you look deeper at almost any program with the graphical interface, you will see that it is only a nice skin that hides the same commands that run when the user clicks buttons. No graphical interface can cower all features of shell commands. So if you start dealing with Linux, then start being a friend to the terminal. Historically Unix computers even didn’t have a graphical interface, so all tasks were performed from the terminal screen. This is…