Here we will learn how to create a database in SQLite using a command-line tool with example and how to use database command in sqlite to create the database with example.
Like other Relational Database Systems, SQLite does not use CREATE DATABASE command to create the database. Instead, it is very simple to create a database in SQLite, and the complete SQLite database is stored in a single cross-platform disk file.
Following is the syntax to create a database in SQLite.
Now we will see how to create a database using the SQLite command-line tool. For that open installed sqlite command-line tool directly or open it from the command prompt.
Open command prompt and navigate to the folder where your sqlite command-line tool installed. During installation, we created the SQLite folder in "C" directory and copied sqlite3.exe in it so now we are navigating to respective SQLite folder like as shown below.
Once we navigated to SQLite installed folder now type the following command to create a database.
By the above command, SQLite creates a database named db1. If there is already a database called db1 then it will open for you.
You can see the database that you have just created by redirecting to sqlite command-line location as follows.
You can see db1.db is created at c:/sqlite location. You can also ensure that the db1 database is created or not by the following command.
From this command, you can see the list of databases created. You can also see db1 on this list.
By default, SQLite creates a new database in a location in which sqlite3.exe is residing. Now, if you create a new database in your preferred location then you can do this by following the below steps.
Go to the folder where your sqlite3.exe is resided like as shown below.
Now double click on sqlite3.exe and run the following command.
This command will create a database named student on desktop folder. Here, it is important to note that .open command is used as two ways. If the database already created on a specific location, then it will open the database otherwise it will create a new one.
Here whenever we run above SQLite command first it will check for student.db in location C:/Users/Admin-PC/Desktop/. If it finds, then it will open the database otherwise it will create a new database named student.db.
This is how we can create databases in SQLite based on our requirements.