SQLite Create Database
SQLite's sqlite3 The command 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
Normally, a database name should be unique within the RDBMS.
In addition, we can also use .open 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.
To open an existing database, the .open command is also used; if the above command test.db exists, it will open directly; if it does not exist, it will be created.
Example
If you want to create a new database
$ 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 directory testDB.db. This file will be used by the SQLite engine as the database. If you have noticed, after successfully creating the database file, the sqlite3 command will provide a sqlite> prompt.
Once the database is created, you can use SQLite's **.databases ** command to check whether it is in the database list, as shown below:
sqlite>.databases
seq name file
--- --------------- ----------------------
0 main /home/sqlite/testDB.db
You can use the SQLite .quit command to exit the sqlite prompt, as shown below:
sqlite>.quit
$
The .dump Command
You can use the SQLite .dump dot command at the command prompt to export the complete database into a text file, as shown below:
$sqlite3 testDB.db .dump > testDB.sql
The above command converts the entire testDB.db database content into SQLite statements and dumps it into an ASCII text file testDB.sql . You can restore from the generated testDB.sql in a simple way, as shown below:
$sqlite3 testDB.db < testDB.sql
The database is empty at this point; once there are tables and data in the database, you can try the above two procedures. Now, let's move on to the next chapter.