Create or Open a SQLite Database

You can create a new database using the command line interface by typing at a shell:

sqlite3 database_name.db

This will create a new database (or open an existing one) named database_name. You do not need to have any special privilege to create a database.

Once a database is created, you can verify it in the list of databases using the following SQLite .databases command.

sqlite>.databases

Finally, you use the SQLite .quit command to come out of the sqlite prompt as follows:

sqlite>.quit
$

Although the usual procedure to create a database in SQL is to use use the statement:

CREATE DATABASE MyDatabase;

this syntax generates an error in SQLite.

Opening an Existing Database with .open

If you want to open multiple databases, or you failed to specify the database to open on the command line, you can also use the .open command in SQLite.

sqlite> .open MyFirstDatabase.db
sqlite> .databases
main: C:sqliteMyFirstDatabase.db
sqlite> .open MySecondDatabase.db
sqlite> .databases
main: C:sqliteMySecondDatabase.db

Reading (a File with SQL Commands) with .read

You can import and process SQL commands from a file. To do this you use the .read command. This processes each line of a file and executes it as a command in SQLite.

This option is useful if you find yourself frequently importing the same data, or creating and populating the same table structures or data directly into SQLite.

In the example below there is a SQL file that creates a things table and populates several records.

sqlite> .open universe.db
sqlite> .tables
sqlite> .read universe-things.sql
sqlite> .tables
things

Such SQL file might just as well contain SQLite-specific commands like

.mode csv
.import path/to/file1.csv table1
.import path/to/file2.csv table2
.import path/to/file2.csv table3
.mode column