SQLite Create Database

SQLite'ssqlite3command is used to create a new SQLite database. You do not need any special permissions to create a database.

Syntax

The basic syntax of the sqlite3 command is as follows:

$ sqlite3 DatabaseName.db

Usually, database names should be unique within the RDBMS.

Alternatively, we can also use.opento create a new database file:

sqlite>.open test.db

The above command creates the database file test.db in the same directory as the sqlite3 command.

To open an existing database, also use.opencommand, if the above commandtest.dbexists, it will be opened directly; if it does not exist, it will be created.

Example

If you want to create a new database <testDB.db>, the SQLITE3 statement is as follows:

$ sqlite3 testDB.db
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

The above command will create a file in the current directorytestDB.db. This file will be used as a database by the SQLite engine. If you have noticed, after the sqlite3 command successfully creates the database file, it will provide asqlite>prompt.

Once the database is created, you can use SQLite's.databases command to check whether it is in the database list, as follows:

sqlite>.databases
seq  name             file
---  ---------------  ----------------------
0    main             /home/sqlite/testDB.db

You can use the SQLite.quitcommand to exit the sqlite prompt, as follows:

sqlite>.quit
$

.dump command

You can use the SQLite.dumpdot command to export the complete database into a text file, as follows:

$sqlite3 testDB.db .dump > testDB.sql

The above command will convert the entiretestDB.dbdatabase content into SQLite statements and dump it into an ASCII text filetestDB.sql. You can restore from the generated testDB.sql in a simple way, as follows:

$sqlite3 testDB.db < testDB.sql

At this point the database is empty. Once there are tables and data in the database, you can try the above two procedures. Now, let's continue to the next chapter.

Other Extensions