46. Working with Databases (SQLite Setup)
For small projects, local storage, or embedded systems, SQLite is the perfect choice. It's a file-based database that requires no separate server process, making it ideal for Termux.
1. Installation
bash $ pkg install sqlite
2. Creating and Accessing a Database
SQLite files are standard files (e.g., mydb.db). You interact with them using the sqlite3 command-line utility.
bash
Creates the file if it doesn't exist and opens the shell
$ sqlite3 my_termux_data.db
3. Basic SQL Commands (Inside the SQLite Shell)
Once inside the shell, you can execute SQL commands.
sql SQLite version 3.45.3 2024-03-01 13:25:00 Enter ".help" for usage hints. sqlite> CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT); sqlite> INSERT INTO users (name, email) VALUES ('Alex', 'alex@termux.com'); sqlite> SELECT * FROM users; 1|Alex|alex@termux.com sqlite> .quit $
Integration with Python
If you are using Python, the built-in sqlite3 module allows you to interact with this database file directly from your Python scripts, providing a seamless mobile data management solution.