Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
Thinkman
Podman vs Docker
MicroPython Robotics
Bottango and Isaaca
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Running K3s on Raspberry Pi
0h 36m
From Docker to Podman
0h 28m
MicroPython Robotics Projects with the Raspberry Pi Pico
0h 24m
Bottango Basics
0h 22m
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
KevsRobots Learning Platform
15% Percent Complete
By Kevin McAleer, 4 Minutes
In Module 2, we will cover the initial steps to set up Python and SQLite3, establish a connection to a database, and perform basic operations using Python’s sqlite3 module.
sqlite3
Before we start working with SQLite databases in Python, we need to ensure that Python and the sqlite3 module are installed on our system.
If Python is not already installed, you can download and install it from the official Python website (https://www.python.org). Choose the appropriate version for your operating system and follow the installation instructions.
Python’s sqlite3 module comes pre-installed with Python, so you don’t need to install any additional packages.
To work with a SQLite database in Python, we first need to establish a connection to the database. The sqlite3 module provides the necessary functions and methods to connect to a database file.
In Python, we use the sqlite3.connect() function to establish a connection to a SQLite database. We pass the path to the database file as an argument.
sqlite3.connect()
import sqlite3 # Connect to a database connection = sqlite3.connect('mydatabase.db')
This code creates a connection to a database file named mydatabase.db. If the file doesn’t exist, SQLite will create a new database file at that location.
mydatabase.db
After we finish working with the database, it’s important to close the connection to release system resources. We can do this by calling the close() method on the connection object.
close()
connection.close()
By closing the connection, we ensure that any changes made to the database are properly saved.
Once we establish a connection to the database, we can execute SQL statements using the connection object.
To create a table in a SQLite database, we use the execute() method and provide a SQL CREATE TABLE statement as a string.
execute()
CREATE TABLE
# Create a table connection.execute(''' CREATE TABLE books ( id INTEGER PRIMARY KEY, title TEXT, author TEXT, year INTEGER ) ''')
In this example, we create a table named books with columns for id, title, author, and year. The id column is defined as the primary key.
books
id
title
author
year
To insert data into a table, we use the execute() method with an SQL INSERT statement.
INSERT
# Insert data into the table connection.execute("INSERT INTO books (title, author, year) VALUES (?, ?, ?)", ("The Great Gatsby", "F. Scott Fitzgerald", 1925))
In this example, we insert a record into the books table, providing values for the title, author, and year columns.
To retrieve data from a table, we use the execute() method with an SQL SELECT statement. We then fetch the results using the fetchall() method.
SELECT
fetchall()
# Query data from the table result = connection.execute("SELECT * FROM books") data = result.fetchall() # Process the retrieved data for row in data: print(row)
This code retrieves all records from the books table and prints each row of data.
When working with databases, it’s essential to handle potential errors and exceptions gracefully. Python’s try and except statements can be used to catch and handle exceptions raised during database operations.
try
except
try: # Database operations connection.execute("...") # ... except sqlite3.Error as e: # Handle the exception print(f"An error occurred: {e}")
By incorporating exception handling, we can provide informative error messages and take appropriate actions in case of any issues.
< Previous Next >
You can use the arrows ← → on your keyboard to navigate between lessons.
← →