sqlite_quick_guide
sqlite_quick_guide
What is SQLite?
SQLite is an in-process library that implements a self-contained, serverless, zero-configuration,
transactional SQL database engine. It is the one database, which is zero-configured, that means
like other database you do not need to configure it in your system.
SQLite engine is not a standalone process like other databases you can link it statically or
dynamically as per your requirement with your application. The SQLite accesses its storage files
directly.
Why SQLite?
SQLite does not require a separate server process or system to operateerverless.
SQLite comes with zero configuration which means no setup or administration needed.
SQLite is very small and light weight, less than 400KiB fully configured or less than 250KiB
with optional features omitted.
SQLite transactions are fully ACID-compliant, allowing safe access from multiple processes or
threads.
SQLite supports most of the query language features found in the SQL92 SQL2 standard.
SQLite is written in ANSI-C and provides simple and easy to use API.
SQLite is available on UNIX Linux, MacOS − X, Android, iOS and Windows Win32, WinCE, WinRT.
History:
1. 2000 -- D. Richard Hipp had designed SQLite for the purpose of no administration required
for operating a program.
3. 2011 -- Hipp announced to add UNQl interface to SQLite DB and to develop UNQLite
Documentorienteddatabase.
SQLITE - INSTALLATION
The SQLite is famous for its great feature zero-configuration, which means no complex setup or
administration is needed. This chapter will take you through the process of setting up SQLite on
Windows, Linux and Mac OS X.
Create a folder C:\>sqlite and unzip above two zipped files in this folder which will give you
sqlite3.def, sqlite3.dll and sqlite3.exe files.
Add C:\>sqlite in your PATH environment variable and finally go to the command prompt
and issue sqlite3 command which should display a result something as below.
C:\>sqlite3
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
$sqlite3
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
If you do not see above result, then it means you do not have SQLite installed on your Linux
machine. So let's follow the following steps to install SQLite:
Above procedure will end with SQLite installation on your Linux machine which you can verify as
explained above.
Above procedure will end with SQLite installation on your Mac OS X machine which you can verify
by issuing following command:
$sqlite3
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
Finally, you have SQLite command prompt where you can issue SQLite commands to do your
exercises.
SQLITE - COMMANDS
Let's start with typing a simple sqlite3 command at command prompt which will provide you
SQLite command prompt where you will issue various SQLite commands.
$sqlite3
SQLite version 3.3.6
Enter ".help" for instructions
sqlite>
For a listing of the available dot commands, you can enter ".help" at any time. For example:
sqlite>.help
Above command will display a list of various important SQLite dot commands, which are as
follows:
Command Description
.dump ?TABLE? Dump the database in an SQL text format. If TABLE specified, only
dump tables matching LIKE pattern TABLE.
.explain ON|OFF Turn output mode suitable for EXPLAIN on or off. With no args, it
turns EXPLAIN on.
.indices ?TABLE? Show names of all indices. If TABLE specified, only show indices
for tables matching LIKE pattern TABLE.
.schema ?TABLE? Show the CREATE statements. If TABLE specified, only show tables
matching LIKE pattern TABLE.
SQLITE - SYNTAX
SQLite is followed by unique set of rules and guidelines called Syntax. This tutorial gives you a
quick start with SQLite by listing all the basic SQLite Syntax:
Case Sensitivity
Important point to be noted is that SQLite is case insensitive but their is some command which is
case sensitive like GLOB and glob have different meaning in SQLite statements.
Comments
SQLite comments are extra notes, which you can add in your SQLite code to increase its
readability and they can appear anywhere whitespace can occur, including inside expressions and
in the middle of other SQL statements but they can not be nested.
SQL comments begin with two consecutive "-" characters ASCII 0x2d and extend up to and
including the next newline character ASCII 0x0a or until the end of input, whichever comes first.
You can also use C-style comments which begin with "/*" and extend up to and including the next
"*/" character pair or until the end of input, whichever comes first. C-style comments can span
multiple lines.
SQLite Statements
All the SQLite statements start with any of the keywords like SELECT, INSERT, UPDATE, DELETE,
ALTER, DROP etc. and all the statements end with a semicolon ;.
SQLite IN Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);
For example:
PRAGMA page_size;
PRAGMA cache_size = 1024;
PRAGMA table_info(table_name);
You would use these data types while creating your tables. SQLite uses a more general dynamic
type system. In SQLite, the datatype of a value is associated with the value itself, not with its
container.
TEXT The value is a text string, stored using the database encoding UTF-
8, UTF-16BE or UTF-16LE
SQLite storage class is slightly more general than a datatype. The INTEGER storage class, for
example, includes 6 different integer datatypes of different lengths.
Affinity Description
TEXT This column stores all data using storage classes NULL, TEXT or
BLOB.
NUMERIC This column may contain values using all five storage classes.
REAL Behaves like a column with NUMERIC affinity except that it forces
integer values into floating point representation
NONE A column with affinity NONE does not prefer one storage class
over another and no attempt is made to coerce data from one
storage class into another.
INTEGER
TINYINT
SMALLINT
MEDIUMINT
BIGINT
INT2
INT8
TEXT
CHARACTER20
VARCHAR255
VARYING
CHARACTER255
NCHAR55
NATIVE
CHARACTER70
NVARCHAR100
TEXT
CLOB
NONE
BLOB
no datatype
specified
REAL
REAL
DOUBLE
DOUBLE PRECISION
FLOAT
NUMERIC
NUMERIC
DECIMAL10,5
BOOLEAN
DATE
DATETIME
Boolean Datatype:
SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as
integers 0 false and 1 true.
Syntax:
Basic syntax of sqlite3 command is as follows:
$sqlite3 DatabaseName.db
Syntax:
Basic syntax of SQLite ATTACH DATABASE statement is as follows:
Above command will also create a database in case database is already not created, otherwise it
will just attach database file name with logical database 'Alias-Name'.
Here 'Alias-Name' is the same alias which you had used while attaching database using ATTACH
statement.
Syntax:
Basic syntax of CREATE TABLE statement is as follows:
CREATE TABLE is the keyword telling the database system to create a new table. The unique name
or identifier for the table follows the CREATE TABLE statement. Optionally you can specify
database_name alongwith table_name.
You have to be careful while using this command because once a table is deleted
then all the information available in the table would also be lost forever.
Syntax:
Basic syntax of DROP TABLE statement is as follows. You can optionally specify database name
along with table name as follows:
Syntax:
There are two basic syntax of INSERT INTO statement is as follows:
Here column1, column2,...columnN are the names of the columns in the table into which you want
to insert data.
You may not need to specify the columns name in the SQLite query if you are adding values for all
the columns of the table. But make sure the order of the values is in the same order as the
columns in the table. The SQLite INSERT INTO syntax would be as follows:
Syntax:
The basic syntax of SQLite SELECT statement is as follows:
Here column1, column2...are the fields of a table whose values you want to fetch. If you want to
fetch all the fields available in the field then you can use following syntax:
SQLITE - OPERATORS
An operator is a reserved word or a character used primarily in an SQLite statement's WHERE
clause to perform operations, such as comparisons and arithmetic operations.
Operators are used to specify conditions in an SQLite statement and to serve as conjunctions for
multiple conditions in a statement.
Arithmetic operators
Comparison operators
Logical operators
Bitwsie operators
Show Examples
Show Examples
Show Examples
Operator Description
AND The AND operator allows the existence of multiple conditions in an SQL
statement's WHERE clause.
BETWEEN The BETWEEN operator is used to search for values that are within a set of
values, given the minimum value and the maximum value.
EXISTS The EXISTS operator is used to search for the presence of a row in a
specified table that meets certain criteria.
LIKE The LIKE operator is used to compare a value to similar values using
wildcard operators.
GLOB The GLOB operator is used to compare a value to similar values using
wildcard operators. Also, GLOB is case sensitive, unlike LIKE.
NOT The NOT operator reverses the meaning of the logical operator with which it
is used. Eg. NOT EXISTS, NOT BETWEEN, NOT IN etc. This is negate
operator.
IS NULL The NULL operator is used to compare a value with a NULL value.
UNIQUE The UNIQUE operator searches every row of a specified table for uniqueness
no duplicates.
p q p&q p|q
0 0 0 0
0 1 0 1
1 1 1 1
1 0 0 1
Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
~A = 1100 0011
The Bitwise operators supported by SQLite language are listed in the following table. Assume
variable A holds 60 and variable B holds 13 then:
Show Examples
& Binary AND Operator copies a bit to the A & B will give 12 which is 0000 1100
result if it exists in both operands.
| Binary OR Operator copies a bit if it exists A | B will give 61 which is 0011 1101
in either operand.
~ Binary Ones Complement Operator is ~A will give -61 which is 1100 0011 in
unary and has the effect of 'flipping' bits. 2's complement form due to a signed
binary number.
<< Binary Left Shift Operator. The left A << 2 will give 240 which is 1111
operands value is moved left by the 0000
number of bits specified by the right
operand.
>> Binary Right Shift Operator. The left A >> 2 will give 15 which is 0000 1111
operands value is moved right by the
number of bits specified by the right
operand.
SQLITE - EXPRESSIONS
An expression is a combination of one or more values, operators, and SQL functions that evaluate
to a value.
SQL EXPRESSIONs are like formulas and they are written in query language. You can also use to
query the database for specific set of data.
Syntax:
Consider the basic syntax of the SELECT statement as follows:
There are different types of SQLite expression, which are mentioned below:
There are several built-in functions like avg, sum, count etc to perform what is known as aggregate
data calculations against a table or a specific table column.
If the given condition is satisfied, means true then it returns specific value from the table. You
would use WHERE clause to filter the records and fetching only necessary records.
The WHERE clause not only used in SELECT statement, but it is also used in UPDATE, DELETE
statement etc. which we would study in subsequent chapters.
Syntax:
The basic syntax of SQLite SELECT statement with WHERE clause is as follows:
Syntax:
The basic syntax of AND operator with WHERE clause is as follows:
You can combine N number of conditions using AND operator. For an action to be taken by the
SQLite statement, whether it be a transaction or query, all conditions separated by the AND must
be TRUE.
The OR Operator:
The OR operator is also used to combine multiple conditions in an SQLite statement's WHERE
clause. While using OR operator, complete condition will be assumed true when atleast any of the
the conditions is true. For example [condition1] OR [condition2] will be true if either condition1 or
condition2 is true.
Syntax:
The basic syntax of OR operator with WHERE clause is as follows:
You can combine N number of conditions using OR operator. For an action to be taken by the
SQLite statement, whether it be a transaction or query, only any ONE of the conditions separated
by the OR must be TRUE.
Syntax:
The basic syntax of UPDATE query with WHERE clause is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Syntax:
The basic syntax of DELETE query with WHERE clause is as follows:
CONCLUSION
If you enjoyed this short and quick tutorial and interested in reading more then I will recommend
you to go through the detailed tutorial because still you are missing many important concepts
related to SQLite.
Processing math: 12%