Friday, September 12, 2025

Python and SQLite

Connecting Python to a SQLite database.

WHAT WE'LL DO

1. Create the SQLite database.

2. Create a database table.

3. Put data in the table.

4. Write a Python script to display the data from the SQLite database.

How you use the data in a program is a different topic.

1. CREATE THE SQLITE DATABASE

A SQLite database is a file. Run the following command:

sqlite3 mydatabase.db


You'll land at the SQLite prompt. At this point, nothing has happened. No database file has been created until you create an object, such as a table.

2. CREATE A DATABASE TABLE

At the sqlite> prompt, run the following command to create a table called person.

create table person (
    id integer primary key autoincrement,
    firstname varchar(64) not null,
    lastname varchar(64) not null,
    email varchar(128),
    phone varchar(32)
);











Notes:
On the data fields
  1. id = a unique identifier for each person.
  2. firstname = the first name of the person.
  3. lastname = the lastname of the person.
  4. email = the email address of the person.
  5. phone = the phone number of the person.
On the data types
  1. integer = a whole number (1, 2, 3 ... ) with no decimals
  2. varchar = a "string" of characters. The number in parenthesis is the length of the string

3. PUT DATA IN THE DATABASE

We're going to add the following four people.

ID First Name Last Name Email Phone
1 John Jones jj@email.com +3.456.1234
2 Peter Panda pete@email.com 123.342312
3 Sally Smooth s.smooth@earth.world (0)567.123
4 Jane Jet jj@spacetechnology.in +(2)-666-889977
 
insert into person (firstname, lastname, email, phone) values
    ("John", "Jones", "jj@email.com", "+3.456.1234"),
    ("Peter", "Panda", "pete@email.com", "123.342312"),
    ("Sally", "Smooth", "s.smooth@earth.world", "(0)567.123"),
    ("Jane", "Jet", "jj@spacetechnology.in", "+(2)-666-889977");

4. WRITE A PYTHON SCRIPT TO DISPLAY THE DATA FROM THE SQLITE DATABASE

The SQL command we need to use to fetch the data is:

select id, firstname, lastname, email phone from person;
        

And you get the following results:








And now for the Python code...

        
#!/bin/env python
import sqlite3

db_connection = sqlite3.connect("mydatabase.db")
db_connection.row_factory = sqlite3.Row
db_cursor = db_connection.cursor()
sql_statement = """
	select id, firstname, lastname, email, phone from person
"""
db_cursor.execute(sql_statement)
data = db_cursor.fetchall()
for row in data:
    print(row["id"], row["firstname"], row["lastname"], row["email"], row["phone"])

db_connection.close()
        

If you write that in a file called data.py, you can run it using the command:

python data.py

And you'll get the following results:













Sunday, September 7, 2025

Managing Python Installations using PyEnv - On Linux

I've been using pyenv on the Mac unwittingly since you're not really supposed to use the Python that ships with the operating system.

So you're forced to install Python from python.org, or as many Mac users who are software developers do, install Homebrew and use that package management system to manage all the software development tools.

Linux and Raspberry Pi

The process is the same.
  1. Update the system.
  2. Install required dependencies for pyenv.
  3. Install pyenv.
  4. Update some environment variables.
  5. Reset the shell
  6. Install a Python version using pyenv.
  7. Set the installed Python version as the default.
  8. Start programming!
In addition to that, I'll also show you how to create a Python virtual environment.

Update the system

sudo apt update

You may need to upgrade your system based on the results of the above command. If so, type the following

sudo apt upgrade

Install required dependencies for pyenv

sudo apt install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git libncursesw5-dev \
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Install pyenv

curl -fsSL https://pyenv.run | bash

Update some environment variables

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc

If you also have a .profile file in your home directory, update that file as well. As well as any .bash_profile or .bash_login files.

Reset the shell

exec "$SHELL"

Or you can logout, and login again.

Install a Python version using pyenv

To see all the commands available in pyenv, type:

pyenv commands

To get help on a specific command (such as install), type:

pyenv install --help

To see ALL the versions of Python that pyenv has available for installation, type:

pyenv install --list

But if you're only interested in the CPython versions starting from 3.0, type:

pyenv install --list | grep -E ' 3\.([1-9][0-9]+)'

Finally, to install a Python version (e.g., 3.13.7), type:

pyenv install 3.13.7

Set the installed Python version as the default

To see all the Python versions that pyenv can manage, type:

pyenv versions

To see the current Python version that's in use, type:

pyenv version

Finally, the easiest way to set the Python version to use (e.g., 3.13.7), type:

pyenv global 3.13.7

pyenv has a couple of other interesting commands for setting the Python version to use depending on whether you just want to use it temporarily in the shell:

pyenv shell 3.13.7

or if you want to use a specific version each time you are in a specific folder (e.g., ~/src/proj1):

cd ~/src/proj1
pyenv local 3.13.7

Now when you switch into that folder, pyenv will make sure to call the correct python version that you specified.

Now all that's left is to...

Start programming!