From Coders.Bay Wiki
Jump to navigation Jump to search


What is SQLite[edit]

SQLite is a software library that provides a relational database management system. The lite in SQLite means lightweight in terms of setup, database administration, and required resources.

SQLite has the following noticeable features: self-contained, serverless, zero-configuration, transactional.


SQLite database is integrated with the application that accesses the database.


SQLite is self-contained means it requires minimal support from the operating system or external library.


Because of the serverless architecture, you don’t need to “install” SQLite before using it. There is no server process that needs to be configured, started, and stopped.


All transactions in SQLite are fully ACID-compliant. It means all queries and changes are Atomic, Consistent, Isolated, and Durable.

SQLite distinctive features[edit]

SQLite uses dynamic types for tables.
SQLite allows a single database connection to access multiple database files simultaneously.

How Tto download SQLite:[edit]

SQLite provides various tools for working across platforms e.g., Windows, Linux, and Mac. You need to select an appropriate version to download.

Installing SQLite[edit]

  • Create a new folder e.g., C:\sqlite
  • Extract the content of the file that you downloaded in the previous section to the C:\sqlite folder. You should see three programs in the C:\sqlite folder as shown below:

Screenshot 1.png

  • Open the command line window:

Screenshot 2.png

  • Navigate to the C:\sqlite folder

Screenshot 3.png

  • Type sqlite3 and press enter, you should see the following output:

Screenshot 4.png

  • You can type the .help command from the sqlite> prompt to see all available commands in sqlite3.

Screenshot 5.png

  • To quit the sqlite>, you use .quit command as follows:

Screenshot 6.png

Connect to an SQLite database[edit]

  • To start the sqlite3, you type the sqlite3 as follows:

Screenshot 7.png

  • To open a database file, you use the .open FILENAME command. The following statement opens the chinook.db database:

Screenshot 8.png

  • If you want to open a specific database file when you connect to the SQlite database, you use the following command:

Screenshot 9.png

  • Following command creates a database named sales in the C:\sqlite\db\ directory:

Screenshot 10.png

  • To show all available commands and their purpose, you use the .help command as follows:

Screenshot 11.png

  • Following command shows all the databases of the current connection:

Screenshot 12.png

  • To add an additional database in the current connection, you use the statement ATTACH DATABASE. The following statement adds the chinook database to the current connection.

Screenshot 13.png

  • Now if you run the .database command again, the sqlite3 returns two databases: main and chinook.

Screenshot 14.png

  • To exit the sqlite3 program, you use the .exit command.

Screenshot 15.png

  • To display all the tables in the current database, you use the .tables command. The following commands open a new database connection to the chinook database and display the tables in the database.

Screenshot 16.png

  • To display the structure of a table, you use the .schema TABLE command. The TABLE argument could be a pattern. If you omit it, the .schema command will show the structures of all the tables. The following command shows the structure of the albums table.

Screenshot 17.png

Save the result of a query into a file[edit]

  • Following commands select the title from the albums table and write the result to the albums.txt file.

Screenshot 18.png

Execute SQL statements from a file[edit]

  • Suppose we have a file named commands.txt in the c:\sqlite\ folder with the following content:

Screenshot 19.png

  • To execute the SQL statements in the commands.txt file, you use the .read FILENAME command as follows:

Screenshot 20.png

SQLite Data Types[edit]

  • NULL - NULL values mean missing information or unknown.
  • INTEGER - Integer values are whole numbers (either positive or negative). An integer can have variable sizes such as 1, 2,3, 4, or 8 bytes.
  • REAL - Real values are real numbers with decimal values that use 8-byte floats.
  • TEXT - TEXT is used to store character data. The maximum length of TEXT is unlimited. SQLite supports various character encodings.
  • BLOB - BLOB stands for a binary large object that can store any kind of data. The maximum size of BLOB is, theoretically, unlimited.

SQL statements[edit]

Simple query[edit]

  • Select – query data from a single table using SELECT statement.

Sorting rows[edit]

  • Order By – sort the result set in ascending or descending order.

Filtering data[edit]

  • Select Distinct – query unique rows from a table using the DISTINCT clause.
  • Where – filter rows of a result set using various conditions.
  • Limit – constrain the number of rows returned by a query and how to get only the necessary data from a table.
  • Between – test whether a value is in a range of values.
  • In – check if a value matches any value in a list of values or subquery.
  • Like – query data based on pattern matching using wildcard characters: percent sign (%) and underscore (_).
  • Glob – determine whether a string matches a specific UNIX-pattern.
  • IS NULL – check if a value is null or not.

Joining tables[edit]

  • Inner Join – query data from multiple tables using the inner join clause.
  • Left Join – combine data from multiple tables using the left join clause.
  • Cross Join – show you how to use the cross join clause to produce a cartesian product of result sets of the tables involved in the join.
  • Self Join – join a table to itself to create a result set that joins rows with other rows within the same table.
  • Full Outer Join – show you how to emulate the full outer join in the SQLite using left join and union clauses.

Grouping data[edit]

  • Group By – combine a set of rows into groups based on specified criteria. The GROUP BY clause helps you summarize data for reporting purposes.
  • Having – specify the conditions to filter the groups summarized by the GROUP BY clause.

Set operators[edit]

  • Union – combine result sets of multiple queries into a single result set. We also discuss the differences between UNION and UNION ALL clauses.
  • Except – compare the result sets of two queries and returns distinct rows from the left query that are not output by the right query.
  • Intersect – compare the result sets of two queries and returns distinct rows that are output by both queries.

More querying techniques[edit]

  • Case – add conditional logic to the query.

Changing data[edit]

  • Insert – insert rows into a table
  • Update – update existing rows in a table.
  • Delete – delete rows from a table.
  • Replace – insert a new row or replace the existing row in a table.

Data definition[edit]

  • Create Table – create a new table in the database.
  • Alter Table – modify the structure of an existing table.
  • Rename column – rename a column of a table.
  • Drop Table – remove a table from the database.
  • VACUUM – optimize database files.


  • Create View – create a new view in the database.
  • Drop View – drop a view from its database schema.


  • Trigger – manage triggers in the SQLite database.
  • Create INSTEAD OF triggers – create an INSTEAD OF trigger to update data via a view.


SQLite Links[edit]

SQLite GUI tools[edit]