4. www.inforepos.com
Install /Configure SQLite
• Unzip both the folders “sqlite-dll-win64-x64-3170000.zip” & “sqlite-tools-
win32-x86-3170000.zip”. Merge all files and place them in one folder, say
“C:sqlite3”
• Add PATH parameter in system environment variables “C:sqlite3”
• Open command prompt and go to “C:sqlite3” run sqlite3.exe
There are two types of command interpreters in SQLite –
• Free-form SQL statement
• Recognized by SQLite program and expects SQL native words at the start of the statement
• Dot-form statement
• Identified by SQLite command-line program (sqlite3.exe) and starts with dot-command
5. www.inforepos.com
SQLite DOT(.) Commands
• A dot-command must begin with the "." at the left margin with no preceding
whitespace.
• The dot-command must be entirely contained on a single input line.
• A dot-command cannot occur in the middle of an ordinary SQL statement. In
other words, a dot-command cannot occur at a continuation prompt.
• Dot-commands do not recognize comments.
• Eg.
• .open
• .tables
• .output
• .schema …. many
6. www.inforepos.com
Accessing Database
OPEN, ATTACH, DETACH database
• ATTACH [DATABASE] filename AS database_name;
• sqlite3>sqlite3 sensorsdb.db
• sqlite3> .database
seq name file
--- --------------- ------------------------
0 main D:sqlite3sensorsdb.db
7. www.inforepos.com
Accessing Database
OPEN, ATTACH, DETACH database
• ATTACH customerdb AS custdb;
• ATTACH DATABASE 'smartihomedb.db' as 'ihomedb';
• ATTACH DATABASE 'smartihealthdb.db' as 'ihealthdb';
• sqlite> .database
seq name file
--- --------------- ------------------------
0 main D:/sqlite3/sensorsdb.db
2 custdb D:/sqlite3/customerdb.db
3 ihomedb D:/sqlite3/smartihomedb.db
4 ihealthdb D:/sqlite3/smartihealthdb.db
9. www.inforepos.com
SQLite Data Types
• A data type specifies a particular type of data, such as integer, floating-point,
Boolean etc.
• A data type also specifies the possible values for that type, the operations that
can be performed on that type and the way the values of that type are stored.
NULL The value is a NULL value.
INTEGER
The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on
the magnitude of the value.
REAL
The value is a floating point value, stored as an 8-byte IEEE floating point
number.
TEXT
The value is a text string, stored using the database encoding (UTF-8, UTF-
16BE or UTF-16LE)
BLOB The value is a blob of data, stored exactly as it was input.
10. www.inforepos.com
SQLite Data Types – Type Affinity Examples
Example Type names From The
CREATE TABLE Statement
or CAST Expression
Resulting Affinity
Rule Used To
Determine Affinity
INT
INTEGER
TINYINT
SMALLINT
MEDIUMINT
BIGINT
UNSIGNED BIG INT
INT2
INT8
INTEGER 1
CHARACTER(20)
VARCHAR(255)
VARYING CHARACTER(255)
NCHAR(55)
NATIVE CHARACTER(70)
NVARCHAR(100)
TEXT
CLOB
TEXT 2
BLOB
no datatype specified
NONE 3
REAL
DOUBLE
DOUBLE PRECISION
FLOAT
REAL 4
NUMERIC
DECIMAL(10,5)
BOOLEAN
DATE | DATETIME
NUMERIC 5
12. www.inforepos.com
SQLite Operators : Highest to Lowest precedence
• ||
• * / %
• + -
• << >> & |
• < <= > >=
• = == != <> IS IS NOT IN LIKE GLOB MATCH REGEXP
• AND
• OR
• Supported unary prefix operators are these: - + ~ NOT
13. www.inforepos.com
DQL
SQLite SELECT Query
• The SELECT statement is used to make a simple query from a database or a
complicated query against some criteria. A SELECT statement does not make
any changes to the database / database objects.
• A query or SELECT statement is a command which gives instructions to a
database to produce certain information(s) from the table in its memory. The
SELECT command starts with the keyword SELECT.
SELECT [ALL | DISTINCT] result [FROM table-list]
[WHERE expr]
[GROUP BY expr-list]
[HAVING expr]
[compound-op select]*
[ORDER BY sort-expr-list]
[LIMIT integer [(OFFSET|,) integer]]
14. www.inforepos.com
DQL
SQLite SELECT Query
• Parameters
Clause Operation perform Input Value
WHERE Used to restrict Expression or condition
DISTINCT Used to restrict List of columns
FROM Joins List of tables
GROUP BY Used to restrict List of columns
ORDER BY List of columns
HAVING Used to restrict Expression or condition
LIMIT Used to restrict Integer value
OFFSET Used to restrict Integer value
15. www.inforepos.com
DQL
SQLite SELECT Query
Basic Arithmetic Operations without any Table:
• SELECT 6+15;
• SELECT 5*15;
• SELECT 5+2-3*4/6;
SELECT Operations Using Tables:
• SELECT * FROM sensor_list;
• SELECT street_address, city FROM locations; (Select required columns)
• SELECT region_name AS "Name of the Region" FROM regions; (Renaming column names)
• SELECT r.region_name FROM regions r; (Select using alias)
• SELECT * FROM locations LIMIT 10; (Limit records)
• SELECT * FROM locations LIMIT 4,5; (Using LIMIT - Skip & Limit records)
• SELECT * FROM locations LIMIT 5 OFFSET 4; (Using LIMIT & OFFSET - Skip & Limit records)
16. www.inforepos.com
DQL
SQLite SELECT Query
• WHERE is a powerful filter, a WHERE clause can let you take exactly the piece of
the data you want. It sets the conditions for the SELECT, and the query will
return only those rows that match the conditions.
• WHERE clause with operators (Arithmetic, Boolean & Comparision)
• SELECT * FROM locations WHERE country_id='CA';
The following operators can be used with WHERE clause:
• operator description
• = Equal
• <> Not equal*
• > Greater than
• < Less than
• >= Greater than or equal
• <= Less than or equal
• SELECT * FROM agents WHERE commission<.15;
17. www.inforepos.com
DQL
SQLite SELECT Query
• WHERE clause with keywords
• DISTINCT
• AND and OR
• ORDER BY column_name(s) ACS | DESC
• GROUP BY
• GROUO BY and ORDER BY
• GROUO BY column_name HAVING
• GROUO BY column_name HAVING expression ORDER BY
• Please follow the notes below for detailed examples.
18. www.inforepos.com
DML
SQLite INSERT INTO
The INSERT command is used to create new rows in the specified table. The INSERT works on a
single table, and can both insert one row at a time or many rows at once using a SELECT
command. This statement is a part of the SQL Data Manipulation Language, DML.
• INSERT [OR conflict-algorithm] INTO [database-name .] table-name [(column-list)] VALUES
(value-list);
• INSERT INTO sensor_list (sensor_id, sensor_name, sensor_type, sensor_usage) VALUES
(‘TMP01’, ‘LM35’, ‘Temperature’, ‘Industrial’);
• INSERT INTO sensor_list (sensor_id, sensor_name, sensor_type) VALUES (‘TMP02’, ‘DHT11’,
‘Temperature’);
• INSERT INTO sensor_list VALUES (‘TMP03’, ‘DHT22’, ‘Temperature’, ‘Regular’);
• INSERT INTO sensor_list (sensor_id, sensor_name) VALUES (‘TMP005’, ‘LM335’);
• INSERT OR REPLACE INTO sensor_list (sensor_id, sensor_name, sensor_type, sensor_usage)
VALUES (‘TMP01’, ‘LM35’, ‘Temperature’, ‘Industrial/Regular’);
19. www.inforepos.com
DML
SQLite UPDATE
The UPDATE command is used to change the existing values to one or more columns of existing rows in a table. This
command can update more than one rows at a time, but all of the rows must be part of the same table.
• UPDATE table_name SET column_name=new_value [, ...] WHERE expression
• UPDATE sensor_list SET sensor_usage='Industrial/Regular' WHERE sensor_usage='Regular';
• UPDATE sensor_list SET sensor_name=(SELECT sensor_name FROM sensor_master WHERE sensor_id =
sensor_list_id);
• Eg. UPDATE sensor_list SET sensor_name=(
SELECT sensor_master.sensor_name
FROM sensor_master
WHERE sensor_list.sensor_list_id=sensor_master.sensor_id),
type_usage=(
SELECT sensor_master.sensor_type || sensor_list.sensor_usage
FROM sensor_master
WHERE sensor_list.sensor_list_id=sensor_master.sensor_id);
20. www.inforepos.com
DML
SQLite DELETE
• The DELETE command is used to delete or remove one or more rows from a
single table. When deleting all rows from the table this will not delete the
structure of the table, it will remain same only the records are deleted. To
delete a table from the database the DROP command is used.
• DELETE FROM [database-name .] table-name [WHERE expr]
• DELETE FROM sensor_list WHERE sensor_id=‘DHT11’;
• DELETE FROM sensor_test;