SQLite Create Database
The sqlite3 command is used to create a new SQLite database. You do not need any special privileges to create a database.
Syntax
The basic syntax of the sqlite3 command is as follows:
$ sqlite3 DatabaseName.db
Typically, the database name should be unique within the RDBMS.
Alternatively, we can also use the .open
command to 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.
The .open
command is also used to open an existing database. If test.db exists, it will open it directly; if it does not exist, it will create it.
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 testDB.db in the current directory. This file will be used by the SQLite engine as the database. If you have noticed, the sqlite3 command provides a sqlite> prompt after successfully creating the database file.
Once the database is created, you can use the SQLite .databases
command to check if it is in the list of databases, as shown below:
sqlite>.databases
seq name file
--- --------------- ----------------------
0 main /home/sqlite/testDB.db
You can exit the sqlite prompt using the SQLite .quit
command, as shown below:
sqlite>.quit
$
.dump Command
You can use the SQLite .dump
command at the command prompt to export the entire database into a text file, as shown below:
$ sqlite3 testDB.db .dump > testDB.sql
The above command converts the entire content of the testDB.db database into SQLite statements and dumps it into the ASCII text file testDB.sql. You can restore from the generated testDB.sql simply 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 proceed to the next chapter.