The reason behind this popularity is some unique characteristics that are unusual and which makes it different from many other SQL database engines like MySQL, PostgreSQL, Oracle, Microsoft SQL Server, etc. Let's get started with it. First, We will install it on Linux and later on cover basic database operations.
Installing Sqlite
- To install on your debian based(Ubuntu, Debian etc.) machine execute below commands.
$ sudo apt-get update $ sudo apt-get install sqlite3
- To install on your RPM based(RHEL, CentOS, Fedora etc.) machine execute below commands.
$ sudo yum update $ sudo yum install sqlite
Now Open a terminal and Execute "sqlite3", you will see the following lines with prompt.
$ sqlite3 SQLite version 3.8.2 2013-12-06 14:53:30 Enter ".help" for instructions Enter SQL statements terminated with a ";"
The very first line shows the version and release date and time of sqlite3.
The second line tells to enter ".help" for instructions.
The second line tells to enter ".help" for instructions.
.help command list all the meta commands and their descriptions. These meta commands are also called "dot" commands because they are preceded by a dot.
Put ".help" to prompt
Put ".help" to prompt
sqlite> .help .backup ?DB? FILE Backup DB (default "main") to FILE .bail ON|OFF Stop after hitting an error. Default OFF .databases List names and files of attached databases .dump ?TABLE? ... Dump the database in an SQL text format If TABLE specified, only dump tables matching LIKE pattern TABLE. .echo ON|OFF Turn command echo on or off .exit Exit this program .explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off. With no args, it turns EXPLAIN on. .header(s) ON|OFF Turn display of headers on or off .help Show this message .import FILE TABLE Import data from FILE into TABLE .indices ?TABLE? Show names of all indices If TABLE specified, only show indices for tables matching LIKE pattern TABLE. .load FILE ?ENTRY? Load an extension library .log FILE|off Turn logging on or off. FILE can be stderr/stdout .mode MODE ?TABLE? Set output mode where MODE is one of: csv Comma-separated values column Left-aligned columns. (See .width) html HTML table code insert SQL insert statements for TABLE line One value per line list Values delimited by .separator string tabs Tab-separated values tcl TCL list elements .nullvalue STRING Use STRING in place of NULL values .open ?FILENAME? Close existing database and reopen FILENAME .output FILENAME Send output to FILENAME .output stdout Send output to the screen .print STRING... Print literal STRING .prompt MAIN CONTINUE Replace the standard prompts .quit Exit this program .read FILENAME Execute SQL in FILENAME .restore ?DB? FILE Restore content of DB (default "main") from FILE .schema ?TABLE? Show the CREATE statements If TABLE specified, only show tables matching LIKE pattern TABLE. .separator STRING Change separator used by output mode and .import .show Show the current values for various settings .stats ON|OFF Turn stats on or off .tables ?TABLE? List names of tables If TABLE specified, only list tables matching LIKE pattern TABLE. .timeout MS Try opening locked tables for MS milliseconds .trace FILE|off Output each SQL statement as it is run .vfsname ?AUX? Print the name of the VFS stack .width NUM1 NUM2 ... Set column widths for "column" mode .timer ON|OFF Turn the CPU timer measurement on or off
Before explaining these dot commands, let's see some of the basic database operations.
First run ".quit" command to end the session.
First run ".quit" command to end the session.
Create Database
Execute below command on shell prompt.
It will create "example.db" (You can put your choice of name in place of "example.db") database file ( if not exists). If exists then open the database contained in the file.
It will create "example.db" (You can put your choice of name in place of "example.db") database file ( if not exists). If exists then open the database contained in the file.
$ sqlite3 example.db SQLite version 3.8.2 2013-12-06 14:53:30 Enter ".help" for instructions Enter SQL statements terminated with a ";"
The difference between "sqlite3" and "sqlite3 dbname.db" command is the first one will create a temporary database for the session and will expire when the session is closed.
Create Table
Let's create a student table with attributes:
reg_no int(4)
Name varchar(20)
Marks int(3)
Name varchar(20)
Marks int(3)
enter the following commands in terminal
sqlite> create table student ( ...> reg_no int(4), ...> name varchar(20), ...> marks int(3) ...> );
It will create a student table with above attributes.
You can see the list of tables in the selected database by .table command
sqlite> .table student
There is only one table student in my case.
You can also see the schema of the table using .schema table_name
sqlite> .schema student CREATE TABLE student ( reg_no int(4), name varchar(20), marks int(3) );
Inserting Data
Let's insert 3 records in the table
101, pradip, 87
102, Avinash, 86
103, Rakesh, 90
101, pradip, 87
102, Avinash, 86
103, Rakesh, 90
Enter following command one by one
sqlite> insert into student (reg_no, name, marks) values (101, 'Pradip', 87); > insert into student (reg_no, name, marks) values (102, 'Avinash', 86); > insert into student (reg_no, name, marks) values (103, 'Rakesh', 91);
Fetching data
You can fetch data from table with select statement
sqlite> select * from student; 101|Pradip|87 102|Avinash|86 103|Rakesh|91
It shows the result in default mode (list).
You can change the mode with .mode command
Enter .mode column to prompt and then perform select query. It will show the result in a column format.
sqlite> .mode column > select * from student; reg_no name marks ---------- ---------- ---------- 101 Pradip 87 102 Avinash 86 103 Rakesh 91
Delete, Alter, Drop etc have same syntax as sql.
Special commands to sqlite3(dot -commands)
we saw ".help" commands list all the dot commands.
Let's understand some other important commands.
Let's understand some other important commands.
- .databases
Shows name of database selected.
sqlite> .database seq name file --- --------------- -------------------- 0 main /home/avi/example.db
- .table
List the tables in the selected database.
sqlite> .table student
- .show
Shows the current settings
sqlite> .show echo: off explain: off headers: off mode: list nullvalue: "" output: stdout separator: "|" stats: off width:
- .header ON|OFF
You can show attribute name in query result by running .header ON|OFF commandsqlite> .header ON > select * from student; reg_no name marks ---------- ---------- ---------- 101 Pradip 87 102 Avinash 86 103 Rakesh 91
- .mode
- .header ON|OFF
The Program is able to show the results of a query in eight different formats: "csv", "column","html","insert", "line", "list", "tabs", "tcl"..mode command is use to switch between these output formats.
sqlite> .mode csv > select * from student; reg_no,name,marks 101,Pradip,87 102,Avinash,86 103,Rakesh,91
You can use .separator command to change separator.
sqlite> .separator -
> select * from student;
reg_no-name-marks
101-Pradip-87
102-Avinash-86
103-Rakesh-91
> select * from student;
reg_no-name-marks
101-Pradip-87
102-Avinash-86
103-Rakesh-91
Writing results to a file
by default, It sends query results to standard output.
you can change this using ".output" and ".once" commands.
you can change this using ".output" and ".once" commands.
Just put the name of an output file as an argument to .output and all subsequent query results will be written to that file.
sqlite> .output ex.txt
Or use the .once the command with file name if you want only the result of next query to be redirected.
We have successfully installed SQLite on Linux with basic operations. These operations are only a few out of all available. We can't cover all of them in this article. If you find any difficulties in an installation or in any command, Let me know in comment section. See you in next article.