reComputerでSQLiteを使用する
はじめに

このwikiでは、reComputerボックスでSQliteを使用する方法を説明します。SQLiteは軽量で組み込み型のリレーショナルデータベース管理システムで、モバイルデバイス、デスクトップアプリケーション、組み込みシステムで広く使用されています。別のサーバープロセスを必要とせず、データベースは単一の通常のディスクファイルに直接保存されます。SQLiteは使いやすく、優れたパフォーマンスを提供します。標準的なSQL構文をサポートし、小規模から中規模のデータストレージニーズに適しています。ゼロ設定と簡単なデプロイメント機能により、SQLiteは多くのプロジェクトで好まれるデータベースエンジンとなっています。
ハードウェアの準備
ソフトウェアの準備
システムの更新
sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
sudo apt update
sudo apt full-upgrade
SQliteのインストール
sudo apt-get install sqlite3

データベースの作成
sqlite3 sensordata.db
.help
コマンドを使用すると、サポートされているすべてのコマンドとそれぞれの使用方法を素早く確認できます。
sqlite> .help

次に.quit
を使用してSQliteシェルを終了します。
sqlite> .quit
SQLの使用
SQLテーブル作成
sqlite3 sensordata.db
次に、以下のようなコマンドを使用して新しいテーブルを作成します。
ain@raspberrypi:~ $ sqlite3 sensordata.db
SQLite version 3.40.1 2022-12-28 14:03:47
Enter ".help" for usage hints.
sqlite> BEGIN;
sqlite> CREATE TABLE dhtreadings (
...> id INTEGER PRIMARY KEY AUTOINCREMENT,
...> temperature NUMERIC,
...> humidity NUMERIC,
...> currentdate DATE,
...> currenttime TIME,
...> device TEXT
...> );
sqlite> COMMIT;
テーブルの確認
以下のコマンドを使用して、作成されたテーブルを表示できます。
sqlite> .tables
dhtreadings
sqlite> .fullschema
CREATE TABLE dhtreadings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
temperature NUMERIC,
humidity NUMERIC,
currentdate DATE,
currenttime TIME,
device TEXT
);
/* No STAT tables available */
SQL挿入
データベースに新しい温度と湿度の読み取り値を挿入するには、次のようにします:
sqlite> BEGIN;
sqlite> INSERT INTO dhtreadings(temperature, humidity, currentdate, currenttime, device) values(22.4, 48, date('now'), time('now'), "manual");
sqlite> COMMIT;
SQL選択
データベースに保存されたデータにアクセスするには、SELECT SQL文を使用します:
sqlite> SELECT * FROM dhtreadings;
1|22.4|48|2025-09-26|01:23:37|manual
これまでのところ、データベースに挿入された読み取り値は1つだけです。次のように新しい読み取り値を挿入できます:
sqlite> BEGIN;
sqlite> INSERT INTO dhtreadings(temperature, humidity, currentdate, currenttime, device) values(22.5, 48.7, date('now'), time('now'), "manual");
sqlite> COMMIT;
そして、テーブルに保存されたデータをSELECTすると、2つの読み取り値が返されます:
sqlite> SELECT * FROM dhtreadings;
1|22.4|48|2025-09-26|01:23:37|manual
2|22.5|48.7|2025-09-26|02:06:35|manual
SQL削除
データベースからテーブルを完全に削除したい場合は、DROP TABLEコマンドを使用できます。
注意:次のコマンドはdhtreadingsテーブルを完全に削除します:
sqlite> DROP TABLE dhtreadings;
今、'.tables'コマンドを入力すると:
sqlite> .tables
テーブルが完全に削除されたため、何も返されません。
PythonでSQLiteを使用する
以下のようにPythonを使用してSQLiteと対話します:
test_sqlite.py
import sqlite3
from datetime import datetime
import os
def create_connection(db_file="dht_readings.db"):
"""Create a database connection to the SQLite database"""
conn = None
try:
conn = sqlite3.connect(db_file)
print(f"Connected to SQLite database: {db_file}")
except sqlite3.Error as e:
print(f"Error connecting to database: {e}")
return conn
def create_table(conn):
"""Create the dhtreadings table if it doesn't exist"""
try:
sql_create_dhtreadings_table = """
CREATE TABLE IF NOT EXISTS dhtreadings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
temperature REAL NOT NULL,
humidity REAL NOT NULL,
currentdate TEXT NOT NULL,
currenttime TEXT NOT NULL,
device TEXT NOT NULL
);
"""
conn.cursor().execute(sql_create_dhtreadings_table)
conn.commit()
print("DHT readings table created successfully")
except sqlite3.Error as e:
print(f"Error creating table: {e}")
def insert_dht_reading(conn, temperature, humidity, device):
"""Insert a new DHT reading into the dhtreadings table"""
sql_insert = """INSERT INTO dhtreadings(temperature, humidity, currentdate, currenttime, device)
VALUES(?, ?, date('now'), time('now'), ?);"""
try:
cursor = conn.cursor()
cursor.execute(sql_insert, (temperature, humidity, device))
conn.commit()
print(f"New record created successfully with ID: {cursor.lastrowid}")
return cursor.lastrowid
except sqlite3.Error as e:
print(f"Error inserting data: {e}")
return None
def select_all_readings(conn):
"""Query all DHT readings in the dhtreadings table"""
try:
cursor = conn.cursor()
cursor.execute("SELECT * FROM dhtreadings ORDER BY currentdate DESC, currenttime DESC")
rows = cursor.fetchall()
print("\nAll DHT readings:")
print("ID | Temperature | Humidity | Date | Time | Device")
print("-" * 60)
for row in rows:
print(f"{row[0]} | {row[1]} | {row[2]} | {row[3]} | {row[4]} | {row[5]}")
except sqlite3.Error as e:
print(f"Error fetching data: {e}")
def main():
# Create a database connection
database = "dht_readings.db"
conn = create_connection(database)
# Create table
if conn is not None:
create_table(conn)
# Insert a sample reading as specified in your requirement
insert_dht_reading(conn, 22.5, 48.7, "manual")
# Insert some additional sample data for testing
insert_dht_reading(conn, 23.1, 45.2, "sensor1")
insert_dht_reading(conn, 21.8, 50.3, "sensor2")
# Display all readings
select_all_readings(conn)
# Close the connection
conn.close()
print("\nDatabase connection closed.")
else:
print("Error! Cannot create database connection.")
if __name__ == '__main__':
main()

技術サポート & 製品ディスカッション
私たちの製品をお選びいただき、ありがとうございます!私たちは、お客様の製品体験ができるだけスムーズになるよう、さまざまなサポートを提供しています。さまざまな好みやニーズに対応するため、複数のコミュニケーションチャネルを提供しています。