SQLite Essentials
Introduction to SQLite
What is SQLite?
Most databases, like MySQL or PostgreSQL, are like big, separate warehouses. Your application sends a request to the warehouse, which then finds the data and sends it back. This requires a server process that's always running, waiting for requests.
SQLite is different. It's more like a personal, high-tech filing cabinet that you build directly into your application. There's no separate server to manage, no complex configuration, and no network communication. The entire database is just a single file on your computer, making it incredibly portable and easy to use.
This design makes SQLite a "serverless," "zero-configuration," and "transactional" SQL database engine. It's self-contained and doesn't depend on any external services.
Because of this simplicity, SQLite is one of the most widely deployed databases in the world. It’s used in mobile phones, web browsers, and countless other applications that need a simple way to store data locally. When you browse the web or use an app on your phone, you're almost certainly using SQLite without even knowing it.
SQLite is a common way provided by the Android SDK for Android apps to persist data.
Getting Started with SQLite
Installing SQLite is straightforward. In fact, you might already have it.
On macOS and Linux:
Open your terminal and type sqlite3 --version. If it’s installed, you’ll see a version number. Most macOS and Linux distributions come with SQLite pre-installed.
On Windows:
If you don't have it, you'll need to download it. Go to the official SQLite website's download page and look for the "Precompiled Binaries for Windows." Download the zip file that contains the command-line tools. Unzip the file, and you'll find sqlite3.exe. To make it easy to run from anywhere, you can move this file to a folder that is in your system's PATH.
Once installed, you can start using SQLite from your command line or terminal. This is the simplest way to interact with your databases. To create a new database (or open an existing one), just run the sqlite3 command followed by the name of the file you want to use.
sqlite3 my_first_database.db
This command does two things: it starts the SQLite command-line shell, and it creates a file named my_first_database.db in your current directory. This file is your database.
You'll see a new prompt that looks like this: sqlite>. You are now inside the SQLite environment, ready to issue commands.
The SQLite shell has special commands that start with a period, often called "dot-commands." They help you manage the environment. For example, you can see a list of all available dot-commands or exit the shell.
sqlite> .help
sqlite> .quit
The .quit command (or .exit) will return you to your regular system terminal. And just like that, you've set up your first SQLite database.