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.
wget http://www.sqlite.org/sqlite-autoconf-3070603.tar.gz
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'
Note: If you are interested in installing MySQL database on your system, you can either use yum groupinstall mysql, or install mysql from rpm.
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 3.7.6.3 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 3.7.6.3 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.
Comments on this entry are closed.
Your tar command under ‘Install SQLite3 from Source’ is only going to list the files – you need to use ‘tar xvfz sqlite-autoconf-3070603.tar.gz’
Good article though.
I use it for learning SQL language.
For
make install
use
sudo make install
Good Work….
While using the command ‘make install’ – user needs to have root access so that folder /usr/local/lib will be created,
if anyone using ubuntu, use sudo -i before using ‘make install’ to achieve this.
thanks.
Thanks.. It s working….
I am working in an office, and I dont have admin rights. So I dont have write access in /use folder so the installation is failing. Is there a workaround to this problem, like just copying an executable and accessing that. Is there a workaround which doesn’t require me to send a request to my cocky IT admin guy
I did it but It was not installed what to do
Hi. This runs perfectly. What if i want add functions like sqlite3_key and sqlite3_rekey to the installations. These functions are present in the source code. But they are not coming in /usr/local/sqlite3.h and linked shared object file for sqlite3. Do i have to pass any flags for ./configure like we pass for sqlcipher? I am using ubuntu machine. Any pointer/direction/keyword will be helpful.
Hi,
Thank you so much for this SQL article.