How to Install and Run SQLite on Android Using Termux

Getting SQLite running in Android Termux opens up some genuinely useful possibilities. Whether you’re building local databases for app development, testing SQL queries on the go, or just learning database management, having SQLite running directly on your phone gives you a portable development environment.

Let me walk you through exactly how I set this up, plus some configuration steps that’ll save you headaches later.

What You Need Before Starting

Termux is a terminal emulator for Android that doesn’t require root access. It gives you a Linux environment right on your phone, which means you can run actual command-line tools like SQLite.

You’ll need:

  • An Android device running Android 7.0 or higher
  • At least 200MB of free storage space
  • The Termux app (grab it from F-Droid, not the Play Store version since that one’s outdated)

Installing SQLite Through Termux Package Manager

Open Termux and first update your package lists. I always do this before installing anything new:

pkg update && pkg upgrade

You’ll see a bunch of text scroll by. Just let it finish. Then install SQLite with a single command:

pkg install sqlite

The package manager handles all dependencies automatically. The whole process takes maybe 30 seconds on a decent connection.

Verify the installation worked:

sqlite3 --version

You should see something like “3.45.1” (the version number changes, but you want to see a version number printed out).

Creating Your First Database File

SQLite stores everything in a single file, which makes it perfect for mobile development. I’ll show you how to create a test database and run some basic operations.

Navigate to a directory where you want to store your database:

cd ~/storage/shared/Documents

Create and open a new database:

sqlite3 mytest.db

You’re now inside the SQLite command prompt. The prompt changes to sqlite> which tells SQLite is listening for commands.

Essential SQLite Commands for Mobile Development

Here’s a practical example. Let me create a simple table for tracking tasks:

CREATE TABLE tasks (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  title TEXT NOT NULL,
  completed INTEGER DEFAULT 0,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP
);

Insert some sample data:

INSERT INTO tasks (title) VALUES ('Learn SQLite basics');
INSERT INTO tasks (title) VALUES ('Build database schema');
INSERT INTO tasks (title, completed) VALUES ('Test queries', 1);

Query your data:

SELECT * FROM tasks;

You’ll see your tasks displayed in a table format. To make the output more readable on a small screen:

.mode column
.headers on
SELECT * FROM tasks;

Configuring SQLite for Better Mobile Experience

The default SQLite settings work, but I’ve found a few tweaks make development on mobile much smoother.

Create a configuration file in your home directory:

nano ~/.sqliterc

Add these lines:

.mode column
.headers on
.nullvalue NULL
.prompt "> "

Save it (Ctrl+X, then Y, then Enter in nano).

These settings persist across sessions, so you don’t need to type them every time you open a database.

Running SQLite Scripts from Files

Writing long SQL statements on a phone keyboard gets tedious fast. I prefer writing queries in separate files and running them as scripts.

Create a SQL file:

nano setup.sql

Add your SQL statements:

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  username TEXT UNIQUE NOT NULL,
  email TEXT
);

INSERT INTO users (username, email) VALUES 
('john_dev', '[email protected]'),
('sarah_test', '[email protected]');

Run the script against your database:

sqlite3 mytest.db < setup.sql

All commands in the file execute in order. You can also run a script from within SQLite:

sqlite> .read setup.sql

Backing Up and Exporting Database Files

SQLite databases are just files, so backing them up is straightforward. I keep copies before making schema changes.

From Termux, copy your database to shared storage:

cp mytest.db ~/storage/shared/Documents/mytest_backup.db

Export as SQL dump (portable across systems):

sqlite3 mytest.db .dump > mytest_dump.sql

Restore from a dump file:

sqlite3 restored.db < mytest_dump.sql

Accessing Android Internal Storage and SD Cards

Termux needs permission to access shared storage. Grant it with:

termux-setup-storage

Accept the permission prompt that appears.

Your Android filesystem becomes accessible at ~/storage/:

  • ~/storage/shared/ – Internal shared storage
  • ~/storage/dcim/ – Camera photos
  • ~/storage/downloads/ – Downloads folder

I keep my development databases in ~/storage/shared/Databases/ for easy access from other apps.

Performance Considerations for Mobile Databases

SQLite on mobile has some quirks compared to desktop development. I’ve learned a few things that help performance:

Use transactions for bulk inserts. Wrapping multiple INSERT statements in a transaction makes them 10-20x faster:

BEGIN TRANSACTION;
INSERT INTO tasks (title) VALUES ('Task 1');
INSERT INTO tasks (title) VALUES ('Task 2');
-- 100 more inserts...
COMMIT;

Create indexes on columns you query frequently:

CREATE INDEX idx_tasks_completed ON tasks(completed);

Set page size before creating tables. The default works, but larger pages help with read performance:

PRAGMA page_size = 4096;
VACUUM;

Using SQLite with Python in Termux

Python’s sqlite3 module works perfectly in Termux. Install Python first:

pkg install python

Create a Python script:

import sqlite3

conn = sqlite3.connect('mytest.db')
cursor = conn.cursor()

cursor.execute('SELECT * FROM tasks WHERE completed = 0')
pending_tasks = cursor.fetchall()

for task in pending_tasks:
    print(f"Task {task[0]}: {task[1]}")

conn.close()

Run it:

python query_tasks.py

I use this approach when I need programmatic database access or want to build simple CLI tools.

Common Issues and Solutions I’ve Encountered

“Database is locked” errors: Another process is accessing the database. Close all connections or use:

PRAGMA busy_timeout = 5000;

Can’t find database file: SQLite creates new databases if the file doesn’t exist. Always check your working directory with pwd before opening a database.

Termux closes and loses work: Android kills background processes aggressively. Install termux-wake-lock to prevent this:

pkg install termux-wake-lock
termux-wake-lock

Corrupted database after unexpected shutdown: Keep backups. Rebuild from your most recent dump file.

Advanced Configuration for Development Workflows

I’ve set up a few aliases to speed up common tasks. Add these to ~/.bashrc:

alias sqldev='sqlite3 ~/storage/shared/Databases/dev.db'
alias sqlprod='sqlite3 ~/storage/shared/Databases/production.db'
alias sqldump='sqlite3 $1 .dump > $1.sql'

Reload your config:

source ~/.bashrc

Now you can open your development database with just sqldev.

Integrating with Android App Development

When building Android apps, you might want to inspect databases created by your app. Apps store SQLite databases in:

/data/data/com.yourapp.package/databases/

You need root access to directly view these. Without root, use Android Debug Bridge (ADB) from Termux:

pkg install android-tools
adb pull /data/data/com.yourapp.package/databases/app.db
sqlite3 app.db

I use this workflow constantly when debugging database issues in apps under development.

Resources for Going Deeper

The official SQLite documentation at sqlite.org covers every function and pragma in detail. I keep it bookmarked.

For learning SQL query patterns, Mode Analytics has a great interactive SQL tutorial that works well alongside hands-on practice in Termux.

The Termux Wiki explains package management and storage access in more depth than I’ve covered here.

SQLite runs remarkably well on Android through Termux. You get a complete relational database system without needing a server, network connection, or complicated setup. I use it for prototyping data models, testing queries before implementing them in apps, and quick data analysis tasks when I’m away from my computer.

The combination of portability and power makes SQLite on Android genuinely useful for development work, not just a novelty. Once you’ve got it configured properly, you have a complete database environment in your pocket.