SQLite3 is an extremely lightweight SQL database engine that is self-contained and serverless.
There is absolutely no configuration that you need to do to get it working. All you need to do is–install it, and start using it.
Since this is serverless, it is used in lot of the famous software that you are using, and you probably didn’t even know those software were using it. View this list to see all the big name companies who are using SQLite. PHP programming language has SQLite database built in.
If you’ve never used SQLite, follow the steps mentioned in this article to install it on Linux, and create a sample database.
Download SQLite3 Source
Go to the SQLite Download page, and click on “sqlite-autoconf-3070603.tar.gz” (Under Source Code section), and download it to your system. Or, use the wget to directly download it to your server as shown below.
Install SQLite3 from Source
Uncompress the tar.gz file and install SQLite3 as shown below.
tar xvfz sqlite-autoconf-3070603.tar.gz cd sqlite-autoconf-3070603 ./configure make make install
make install command will displays the following output indicating that it is installing sqlite3 binaries under /usr/local/bin
test -z "/usr/local/bin" || mkdir -p -- "/usr/local/bin" ./libtool --mode=install /usr/bin/install -c sqlite3 /usr/local/bin/sqlite3 /usr/bin/install -c .libs/sqlite3 /usr/local/bin/sqlite3 test -z "/usr/local/include" || mkdir -p -- "/usr/local/include" /usr/bin/install -c -m 644 'sqlite3.h' '/usr/local/include/sqlite3.h' /usr/bin/install -c -m 644 'sqlite3ext.h' '/usr/local/include/sqlite3ext.h' test -z "/usr/local/share/man/man1" || mkdir -p -- "/usr/local/share/man/man1" /usr/bin/install -c -m 644 './sqlite3.1' '/usr/local/share/man/man1/sqlite3.1' test -z "/usr/local/lib/pkgconfig" || mkdir -p -- "/usr/local/lib/pkgconfig" /usr/bin/install -c -m 644 'sqlite3.pc' '/usr/local/lib/pkgconfig/sqlite3.pc'
Create a sample SQLite database
The example shown below does the following:
- Create a new SQLite database called “company.db”.
- Create “employee” table with three fields 1) Employee Id 2) Name and 3) Title
- Insert 5 records into the employee tables.
- Verify the records
- Exit SQLite3
$ sqlite3 company.db SQLite version 184.108.40.206 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> create table employee(id integer,name varchar(20),title varchar(10)); sqlite> insert into employee values(101,'John Smith','CEO'); sqlite> insert into employee values(102,'Raj Reddy','Sysadmin'); sqlite> insert into employee values(103,'Jason Bourne','Developer'); sqlite> insert into employee values(104,'Jane Smith','Sale Manager'); sqlite> insert into employee values(104,'Rita Patel','DBA'); sqlite> select * from employee; 101|John Smith|CEO 102|Raj Reddy|Sysadmin 103|Jason Bourne|Developer 104|Jane Smith|Sale Manager 104|Rita Patel|DBA sqlite>[Press Ctrl-D to exit]
Access the SQLite Database
When you create a database, it is nothing but a file. If you do “ls”, you’ll see the “company.db” file as shown below.
$ ls -l company.db -rw-r--r--. 1 ramesh ramesh 2048 Jun 18 21:27 company.db
To access an existing database and query the records, do the following. i.e When you do “sqlite3 company.db”, if the database doesn’t exist it’ll create it. If it already exists, it’ll open it.
$ sqlite3 company.db SQLite version 220.127.116.11 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> select * from employee; 101|John Smith|CEO 102|Raj Reddy|Sysadmin 103|Jason Bourne|Developer 104|Jane Smith|Sale Manager 104|Rita Patel|DBA sqlite>[Press Ctrl-D to exit]
This is just a jumpstart guide for you to get started on SQLite3. In our future articles about SQLite3, we’ll be discussing about several SQLite3 commands, how to access the SQLite3 database from various programming languages, and several tips and tricks on SQLite3.
Get the Linux Sysadmin Course Now!