SQLite Database: How to Create, Open, Backup & Drop Files

⚡ Smart Summary

SQLite databases store an entire dataset inside a single lightweight file, requiring no server, installation, or configuration. The sqlite3 command-line tool creates, opens, populates, backs up, and removes these database files directly from a simple terminal session.

  • 🗄️ No CREATE DATABASE command: SQLite has no CREATE DATABASE statement; running sqlite3 SchoolDB.db creates the file automatically.
  • 📂 Specific location: The .open command creates or opens a database at any path you name.
  • 🧱 Schema from a file: Piping a .sql file into sqlite3 builds a new database already populated with its tables.
  • 💾 Backup: The .backup command copies an open database into a new file for safekeeping.
  • 🗑️ Dropping a database: SQLite has no DROP DATABASE command; deleting the database file removes it entirely.
  • 🤖 AI assistance: GitHub Copilot and text-to-SQL tools generate SQLite schemas and queries from plain-English prompts.

SQLite Database

SQLite databases are very lightweight. Unlike other database systems, no configuration or installation is required to start working on an SQLite database. All you need is the SQLite library, which is under 500 KB, so you can begin working with SQLite databases and tables directly.

SQLite CREATE Database

Unlike other database management systems, there is no CREATE DATABASE command in SQLite. Here is how you can create a new database:

  • Open the Windows Command Line tool (cmd.exe) from the Start menu, type “cmd”, and open it.
  • The cmd window will open in the default user folder. On my machine, it is “C:\Users\MGA”:

SQLite command prompt opened in the default C:/Users/MGA user folder

  • From the Installation and packages tutorial, you should now have created an SQLite folder in the “C” directory and copied sqlite3.exe into it.
  • Next, navigate to where sqlite3.exe is located using the following SQLite command line:

Navigating to the sqlite3.exe folder in the Windows command line

Following is the basic syntax of the sqlite3 command to create a database:

sqlite3 SchoolDB.db

This will create a new database named “SchoolDB.db” in the same directory where you copied your .exe file.

If you navigate to the directory “c:\sqlite”, you will find that the file “SchoolDB.db” has been created, as in the following screenshot:

Running the sqlite3 SchoolDB.db command to create the database

The SchoolDB.db database file created in the c:/sqlite directory

You can confirm that the database is created by writing the following SQLite command:

.databases

This gives you the list of databases created, and you should see the new database “SchoolDB.db” listed there:

The .databases command listing the newly created SchoolDB database

SQLite CREATE Database in a Specific Location using Open

If you want to create the database file in a specific location rather than in the folder where sqlite3.exe is located, here is how to open an SQLite database file:

  • Navigate manually to the folder where sqlite3.exe is located, “C:\sqlite”.
  • Double-click sqlite3.exe to open the SQLite command line.
  • The command to open a database file is:

Opening an SQLite database file in a specific location with the .open command

.open c:/users/mga/desktop/SchoolDB.db

This creates the database “SchoolDB.db” and stores the file in the specified location. The same command also opens an existing database, so running it again simply reopens that database:

.open c:/users/mga/desktop/SchoolDB.db

SQLite checks whether “SchoolDB.db” exists in that location. If it exists, SQLite opens it; otherwise, a new database is created with that file name.

SQLite create a database and populate it with tables from a file

If you have a .SQL file that contains the table schema and you want to create a new database with those same tables, the following example explains how to do this.

Example:

Here we create the sample database used throughout this SQLite tutorial, named “SQLiteTutorialsDB”, and populate it with tables as follows:

  • Open a text file and paste the following SQLite commands into it:
CREATE TABLE [Departments] ( 
	[DepartmentId] INTEGER  NOT NULL PRIMARY KEY, 
	[DepartmentName] NVARCHAR(50)  NOT NULL  
); 
CREATE TABLE [Students] (  
	[StudentId] INTEGER  PRIMARY KEY NOT NULL,
	[StudentName] NVARCHAR(50) NOT NULL, 
	[DepartmentId] INTEGER  NULL,   
	[DateOfBirth] DATE  NULL  
);     
CREATE TABLE [Subjects] (  
	[SubjectId] INTEGER  NOT NULL PRIMARY KEY,  
	[SubjectName] NVARCHAR(50)  NOT NULL  
); 
CREATE TABLE [Marks] (  
	[StudentId] INTEGER  NOT NULL,  
	[SubjectId] INTEGER  NOT NULL,  
	[Mark] INTEGER  NULL  
);

The code above creates four tables, as follows:

  • “Departments” table, with the following columns:
    • “DepartmentId”, an integer department id, declared as a PRIMARY KEY.
    • “DepartmentName”, a string department name that does not allow null values (NOT NULL).
  • “Students” table, with the following columns:
    • “StudentId”, an integer declared as a PRIMARY KEY.
    • “StudentName”, the student name, which does not allow a null value.
    • “DepartmentId”, an integer referring to the department id in the Departments table.
    • “DateOfBirth”, the date of birth of the student.
  • “Subjects” table, with the following columns:
    • “SubjectId”, an integer declared as a PRIMARY KEY.
    • “SubjectName”, a string value that does not allow null values.
  • “Marks” table, with the following columns:
    • “StudentId”, an integer indicating a student id.
    • “SubjectId”, an integer indicating a subject id.
    • “Mark”, the mark a student gets in a subject, an integer that allows null values.
  • Save the file as “SQLiteTutorialsDB.sql” in the same location as sqlite3.exe.
  • Open cmd.exe and navigate to the directory where sqlite3.exe is located.
  • Write the following command:
sqlite3 SQLiteTutorialsDB.db < SQLiteTutorialsDB.sql

A new database “SQLiteTutorialsDB” is created, and the file “SQLiteTutorialsDB.db” is placed in the same directory, as follows:

The SQLiteTutorialsDB database created from the .sql schema file

You can confirm the tables are created by opening the database we just created:

.open SQLiteTutorialsDB.db

Then write the following command:

.tables

This gives you the list of tables in “SQLiteTutorialsDB”, and you should see the four tables we just created:

The .tables command listing the four tables created in SQLiteTutorialsDB

With the sample database and its four tables in place, you can move on to running SQLite INSERT, UPDATE, and DELETE queries against the data.

SQLite Backup & Database

To back up a database, you have to open that database first, as follows:

  • Navigate to the “C:\sqlite” folder, then double-click sqlite3.exe to open it.
  • Open the database using the following query:
.open c:/sqlite/sample/SchoolDB.db

This command opens a database located in the directory “c:/sqlite/sample/”.

If it is in the same directory as sqlite3.exe, then you do not need to specify a location, like this:

.open SchoolDB.db

Then, to back up the database, write the following command. This backs up the whole database into a new file “SchoolDB.db” in the same directory:

.backup SchoolDB.db

If you do not see any errors after executing that command, the backup has been created successfully.

SQLite Drop Database

Unlike other database management systems, there is no DROP DATABASE command in SQLite. If you want to drop a database in SQLite, all you have to do is delete the database file.

Notes:

  • You cannot create two databases in the same location with the same name; the database name is unique within a directory.
  • Database names are case insensitive.
  • There are no privileges required to create the databases.

FAQs

SQLite is a serverless engine that keeps an entire database in one cross-platform file. Under 500 KB, it needs no installation or configuration and runs embedded inside applications rather than on a separate server.

SQLite databases commonly use the .db, .sqlite, or .db3 extension, and all three are identical. SQLite identifies its files by an internal header, so the extension is only a label.

Yes. SQLite is public-domain software, completely free for personal and commercial use with no license or royalty. Its source code and command-line tools are on the official SQLite website.

Open sqlite3 with a database name and run .restore with the backup file. For a .sql dump, run sqlite3 newdb.db < backup.sql to replay every statement and rebuild the data.

SQLite is serverless and file-based, needing no setup, so it suits small single-user apps. MySQL runs a server process and handles many concurrent writers, fitting larger multi-user systems.

SQLite is embedded in mobile apps, browsers, desktop software, and IoT devices because it works offline with no server. It suits local storage, prototypes, and read-heavy workloads.

Yes. GitHub Copilot writes CREATE TABLE statements and SQL queries from a short comment or plain-English prompt, and explains existing schemas. Always review generated SQL before running it.

Text-to-SQL tools and large language models turn plain-English questions into SQLite statements by reading your schema. AI assistants generate, optimize, and explain queries, though human review of the output remains essential.

Summarize this post with: