DB Python Unit 1 & 2
DB Python Unit 1 & 2
What is SQLite?
SQLite is free to use for any purpose commercial or private. In other words, "SQLite is an
open source, zero-configuration, self-contained, stand alone, transaction relational database
engine designed to be embedded into an application".
SQLite is different from other SQL databases because unlike most other SQL databases,
SQLite does not have a separate server process. It reads and writes directly to ordinary disk
files. A complete SQL database with multiple tables, indices, triggers, and views, is contained
in a single disk file.
SQL SQLite
SQL is a standard which specifies how a SQLite is file-based. It is different from other SQL
relational schema is created, data is inserted databases because unlike most other SQL
or updated in the relations, transactions are databases, SQLite does not have a separate
started and stopped, etc. server process.
Main components of SQL are Data SQLite supports many features of SQL and has
Definition Language(DDL) , Data high performance and does not support stored
Manipulation Language(DML), Embedded procedures.
SQL and Dynamic SQL.
SQL is Structured Query Language which SQLite is a portable database resource. You have
is used with databases like MySQL, Oracle, to get an extension of SQLite in whatever
Microsoft SQL Server, IBM DB2, etc. It is language you are programming in to access that
not a database itself. database. You can access all of the desktop and
mobile applications.
A conventional SQL database needs to be SQLite database system doesn?t provide such
running as a service like OracleDB to functionalities.
connect to and provide a lot of
functionalities.
SQL is a query language which is used by SQLite is a database management system itself
different SQL databases. It is not a database which uses SQL.
itself.
SQLite was designed originally on August 2000. It is named SQLite because it is very light
weight (less than 500Kb size) unlike other database management systems like SQL Server or
Oracle.
Year Happenings
2000 SQLite was designed by D. Richard Hipp for the purpose of no administration
required for operating a program.
2011 Hipp announced to add UNQl interface to SQLite db and to develop UNQLite
(Document oriented database).
Following is a list of features which makes SQLite popular among other lightweight
databases:
o SQLite is totally free: SQLite is open-source. So, no license is required to work with
it.
o SQLite is serverless: SQLite doesn't require a different server process or system to
operate.
o SQLite is very flexible: It facilitates you to work on multiple databases on the same
session on the same time.
o Configuration Not Required: SQLite doesn't require configuration. No setup or
administration required.
o SQLite is a cross-platform DBMS: You don't need a large range of different
platforms like Windows, Mac OS, Linux, and Unix. It can also be used on a lot of
embedded operating systems like Symbian, and Windows CE.
SQLite Advantages
SQLite is a very popular database which has been successfully used with on disk file format
for desktop applications like version control systems, financial analysis tools, media
cataloging and editing suites, CAD packages, record keeping programs etc.
1) Lightweight
2) Better Performance
o Reading and writing operations are very fast for SQLite database. It is almost 35%
faster than File system.
o It only loads the data which is needed, rather than reading the entire file and hold it in
memory.
o If you edit small parts, it only overwrite the parts of the file which was changed.
o SQLite is very easy to learn. You don?t need to install and configure it. Just download
SQLite libraries in your computer and it is ready for creating the database.
4) Reliable
o It updates your content continuously so, little or no work is lost in a case of power
failure or crash.
o SQLite is less bugs prone rather than custom written file I/O codes.
o SQLite queries are smaller than equivalent procedural codes so, chances of bugs are
minimal.
5) Portable
o SQLite is portable across all 32-bit and 64-bit operating systems and big- and little-
endian architectures.
o Multiple processes can be attached with same application file and can read and write
without interfering each other.
o It can be used with all programming languages without any compatibility issue.
6) Accessible
o It reduces application cost because content can be accessed and updated using concise
SQL queries instead of lengthy and error-prone procedural queries.
o SQLite can be easily extended in in future releases just by adding new tables and/or
columns. It also preserve the backwards compatibility.
The above method facilitates you a permanent way to create database, attach database and
detach database.
There is another way to execute CRUD operation in SQLite. In this method, there is no need
to set a path.
You can execute the SQLite query here. But here, the data is temporary and once you shut
down your computer, you will lose the records you have. Because, you cannot create, attach
or detach a database here.
SQLite data types are used to specify type of data of any object. Each column, variable and
expression has related data type in SQLite. These data types are used while creating table.
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.
SQLite Storage Classes : The stored values in a SQLite database has one of the following
storage classes:
Storage Description
Class
REAL It specifies the value is a floating point value, stored as an 8-byte IEEE floating
point number.
text It specifies the value is a text string, stored using the database encoding (utf-8,
utf-16be or utf-16le)
BLOB It specifies the value is a blob of data, stored exactly as it was input.
SQLite supports type affinity for columns. Any column can still store any type of data but the
preferred storage class for a column is called its affinity.
Affinity Description
TEXT This column is used to store all data using storage classes NULL, TEXT or
BLOB.
NUMERIC This column may contain values using all five storage classes.
INTEGER It behaves the same as a column with numeric affinity with an exception in a cast
expression.
REAL It 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
don't persuade data from one storage class into another.
Following is a list of various data types names which can be used while creating SQLite
tables.
INT INTEGER
INTEGER
TINYINT
SMALLINT
MEDIUMINT
BIGINT
INT2
INT8
CHARACTER(20) TEXT
VARCHAR(255)
VARYING
CHARACTER(255)
NCHAR(55)
NATIVE CHARACTER(70)
NVARCHAR(100)
TEXT
CLOB
REAL REAL
DOUBLE
DOUBLE PRECISION
FLOAT
NUMERIC NUMERIC
DECIMAL(10,5)
BOOLEAN
DATE
DATETIME
In SQLite, there is no separate class to store dates and times. But you can store date and times
as TEXT, REAL or INTEGER values.
REAL It specifies the number of days since noon in Greenwich on November 24,
4714 B.C.
In SQLite, there is not a separate Boolean storage class. Instead, Boolean values are stored as
integers 0 (false) and 1 (true).
By default, SQLite operates in auto-commit mode. It means that for each command, SQLite
starts, processes, and commits the transaction automatically.
BEGIN TRANSACTION;
After executing the statement BEGIN TRANSACTION, the transaction is open until it is
explicitly committed or rolled back.
Second, issue SQL statements to select or update data in the database. Note that the change is
only visible to the current session (or client).
Third, commit the changes to the database by using the COMMIT or COMMIT
TRANSACTION statement.
COMMIT;
If you do not want to save the changes, you can roll back using
the ROLLBACK or ROLLBACK TRANSACTION statement:
ROLLBACK;
We will create two new tables: accounts and account_changes for the demonstration.
The accounts table stores data about the account numbers and their balances.
The account_changes table stores the changes of the accounts.
First, create the accounts and account_changes tables by using the following CREATE
TABLE statements:
CHECK(balance >= 0)
);
VALUES (100,20100);
VALUES (200,10100);
BEGIN TRANSACTION;
UPDATE accounts
UPDATE accounts
VALUES(100,'-',1000,datetime('now'));
VALUES(200,'+',1000,datetime('now'));
COMMIT;
BEGIN TRANSACTION;
UPDATE accounts
VALUES(100,'-',20000,datetime('now'));
ROLLBACK;
Finally, query data from the account_changes table, you will see that the change no #3 is not
there anymore:
1.2.1 Filtering: Distinct, where, between, in, like, Union, intersect, Except, Limit, IS
NULL
DISTINCT clause
The DISTINCT clause is an optional clause of the SELECT statement.
The DISTINCT clause allows you to remove the duplicate rows in the result set.
The following statement illustrates the syntax of the DISTINCT clause:
Where clause
The WHERE clause is an optional clause of the SELECT statement. It appears after
the FROM clause as the following statement:
Syntax:
SELECT
column_list
FROM
table
WHERE
search_condition;
Example:
Select * from emp where designation= “Manager”;
BETWEEN
BETWEEN operator is a logical operator that tests whether a value is in range of values. If
the value is in the specified range, the BETWEEN operator returns true.
The BETWEEN operator can be used in the WHERE clause of
the SELECT, DELETE, UPDATE, and REPLACE statement
in
The SQLite IN operator determines whether a value matches any value in a list
Example:
not in
like
To query data based on partial information, you use the LIKE operator in the WHERE clause
of the SELECT statement as follows:
SELECT
column_list
FROM
table_name
WHERE
column_1 LIKE pattern;
SQLite provides two wildcards for constructing patterns. They are percent sign % and
underscore _ :
Example:
SELECT s_name FROM student WHERE s_name LIKE 'M%';
union
Sometimes, you need to combine data from multiple tables into a complete result set. It may
be for tables with similar data within the same database or maybe you need to combine
similar data from multiple databases.
To combine rows from two or more queries into a single result set, you use
SQLite UNION operator.
Example:
SELECT FirstName, LastName, 'Employee Name' AS Type FROM employees UNION
SELECT FirstName, LastName, 'Customer' FROM customers;
SQLite INTERSECT operator compares the result sets of two queries and returns distinct
rows that are output by both queries.
The following illustrates the syntax of the INTERSECT operator:
Syntax:
SELECT select_list1
FROM table1
INTERSECT
SELECT select_list2
FROM table2
Example:
SELECT CustomerId, cname FROM customers INTERSECT
SELECT CustomerId,cname FROM invoices ;
Except
SQLite EXCEPT operator compares the result sets of two queries and returns distinct rows
from the left query that are not output by the right query.
The following shows the syntax of the EXCEPT operator:
Syntax:
SELECT select_list1 FROM table1
EXCEPT
SELECT select_list2 FROM table2
Example:
SELECT ArtistId FROM artists EXCEPT SELECT ArtistId FROM albums;
Limit
The LIMIT clause is an optional part of the SELECT statement. You use the LIMIT clause to
constrain the number of rows returned by the query.
Syntax:
IS NULL
NULL is special. It indicates that a piece of information is unknown or not applicable.
Example:
SELECT Name, Composer FROM tracks WHERE Composer = NULL;
You often use the HAVING clause with the GROUP BY clause. The GROUP BY clause
groups a set of rows into a set of summary rows or groups. Then the HAVING clause filters
groups based on a specified condition.
If you use the HAVING clause, you must include the GROUP BY clause; otherwise, you will
get the error.
Example:
SELECT albumid, COUNT(trackid) FROM tracks GROUP BY albumid
HAVING albumid = 5;
The GROUP BY clause is an optional clause of the SELECT statement. The GROUP
BY clause a selected group of rows into summary rows by values of one or more columns.
The GROUP BY clause returns one row for each group. For each group, you can apply an
aggregate function such as MIN, MAX, SUM, COUNT, or AVG to provide more
information about each group
The SQLite CASE expression evaluates a list of conditions and returns an expression based
on the result of the evaluation.
The CASE expression is similar to the IF-THEN-ELSE statement in other programming
languages.
You can use the CASE expression in any clause or statement that accepts a valid expression.
For example, you can use the CASE expression in clauses such as WHERE, ORDER
BY, HAVING, SELECT and statements such as SELECT, UPDATE, and DELETE.
Syntax of SQLite
Case Statement Following is the syntax of the SQLite CASE statement.
CASE test_expression
WHEN [condition.1] THEN [expression.1]
WHEN [condition.2] THEN [expression.2] ...
WHEN [condition.n] THEN [expression.n]
ELSE [expression]
END
In the above SQLite Case Statement syntax, we defined multiple conditions to get the
required values. Here in SQLite Case statement each WHEN. .. THEN clauses evaluated in
an orderly manner. First, it evaluated condition 1 in case if it satisfied then it returns
expression 1 otherwise it will execute condition 2 and so on. If no condition is satisfied, then
finally execution goes to ELSE block, and expression under ELSE is evaluated.
We will see how to use the SQLite case statement with Select query for that create one table
called STUDENT and insert some data by using the following queries.
CREATE TABLE STUDENT (ID INTEGER PRIMARY KEY, NAME TEXT NOT NULL,
EMAIL TEXT, PER FLOAT)
Once we create and insert data in the STUDENT table execute the following query to check
records in the table.
Following is the example of using the SQLite Case Statement with Select query.
In the above SQLite Case statement example, we added multiple conditions using
a case statement to get Grade of the student based on the marks.
In this example first, it checks that marks greater than 80 if so then it will print grade “A+”, if
not it will check whether marks greater than 70 if so then it will print grade “A”, if not then it
checks for marks greater than 60 if so then it will print grade “B” and not then it checks for
marks greater than 50 if so then it will print grade “C” and if no condition satisfy then it will
print “Sorry!! Failed”.
Now we will run and check the result of the above query that will be like as shown below.
Example:
An artist can have zero or many albums while an album belongs to one artist.
To query data from both artists and albums tables, you use can use an INNER JOIN, LEFT
JOIN, or CROSS JOIN clause. Each join clause determines how SQLite uses data from one
table to match with rows in another table.
Note that SQLite doesn’t directly support the RIGHT JOIN and FULL OUTER JOIN.
INNER JOIN
The following statement returns the album titles and their artist names:
Example:
SELECT Title, Name FROM albums INNER JOIN artists ON artists.ArtistId =
albums.ArtistId;
LEFT JOIN
This statement selects the artist names and album titles from the artists and albums tables
using the LEFT JOIN clause:
Example:
SELECT Name, Title FROM artists LEFT JOIN albums ON artists.ArtistId =
albums.ArtistId ORDER BY Name;
CROSS JOIN
The CROSS JOIN clause creates a Cartesian product of rows from the joined tables.
Example:
SELECT * FROM products CROSS JOIN calendars;
Self join
The self-join is a special kind of joins that allow you to join a table to itself using either
LEFT JOIN or INNER JOIN clause. You use self-join to create a result set that joins the rows
with the other rows within the same table.
The self join, as its name implies, joins a table to itself. To use a self join, the table must
contain a column (call it X) that acts as the primary key and a different column (call it Y) that
stores values that can be matched up with the values in Column X. The values of Columns X
and Y do not have to be the same for any given row, and the value in Column Y may even
be null.
Let’s take a look at an example. Consider the table Employees:
X Y
Each employee has his/her own Id, which is our “Column X.” For a given employee (i.e.,
row), the column ManagerId contains the Id of his or her manager; this is our “Column Y.” If
we trace the employee-manager pairs in this table using these columns:
The manager of the employee John Smith is the employee with Id 3, i.e., Tom Lanon.
This type of table structure is very common in hierarchies. Now, to show the name of the
manager for each employee in the same row, we can run the following query:
The query selects the columns Id, FullName, and ManagerId from the table
aliased employee. It also selects the FullName column of the table aliased manager and
designates this column as ManagerName. As a result, every employee who has a manager is
output along with his/her manager’s ID and name.
In this query, the Employees table is joined with itself and has two different roles:
Role 1: It stores the employee data (alias employee).
Role 2: It stores the manager data (alias manager).
By doing so, we are essentially considering the two copies of the Employees table as if they
are two distinct tables, one for the employees and another for the managers.
The following statement uses the FULL OUTER JOIN clause to query data from
the dogs and cats tables.
ON table_name
[WHEN condition]
BEGIN
statements;
END;
First, specify the name of the trigger after the CREATE TRIGGER keywords.
Next, determine when the trigger is fired such as BEFORE, AFTER, or INSTEAD
OF. You can create BEFORE and AFTER triggers on a table. However, you can only
create an INSTEAD OF trigger on a view.
Then, specify the event that causes the trigger to be invoked such
as INSERT, UPDATE, or DELETE.
After that, indicate the table to which the trigger belongs.
Finally, place the trigger logic in the BEGIN END block, which can be any valid SQL
statements.
If you combine the time when the trigger is fired and the event that causes the trigger to be
fired, you have a total of 9 possibilities:
BEFORE INSERT
AFTER INSERT
BEFORE UPDATE
AFTER UPDATE
BEFORE DELETE
AFTER DELETE
INSTEAD OF INSERT
INSTEAD OF DELETE
INSTEAD OF UPDATE
Suppose you use a UPDATE statement to update 10 rows in a table, the trigger that
associated with the table is fired 10 times. This trigger is called FOR EACH ROW trigger. If
the trigger associated with the table is fired one time, we call this trigger a FOR EACH
STATEMENT trigger.
As of version 3.9.2, SQLite only supports FOR EACH ROW triggers. It has not yet
supported the FOR EACH STATEMENT triggers.
If you use a condition in the WHEN clause, the trigger is only invoked when the condition is
true. In case you omit the WHEN clause, the trigger is executed for all rows.
For example, a trigger references to a table named people, you drop the people table or
rename it, you need to manually change the definition of the trigger.
You can access the data of the row being inserted, deleted, or updated using
the OLD and NEW references in the form: OLD.column_name and NEW.column_name.
the OLD and NEW references are available depending on the event that causes the trigger to
be fired.
Action Reference
We used the NEW reference to access the email column of the row that is being inserted.
To validate the email, we used the LIKE operator to determine whether the email is valid or
not based on the email pattern. If the email is not valid, the RAISE function aborts the insert
and issues an error message.
Second, insert a row with an invalid email into the leads table.
INSERT INTO leads (first_name, last_name, email, phone) VALUES(' Ravi’, 'Bhatt','jjj',
‘6767690’);
SQLite issued an error: “Invalid email address” and aborted the execution of the insert.
Third, insert a row with a valid email.
Demo:
CREATE TRIGGER
trg_validate_products_before_insert
BEFORE INSERT
ON Product
BEGIN
SELECT
CASE
WHEN NEW.quantity < 0 THEN
RAISE(ABORT, 'Invalid Quantity')
END;
In above statement, we used a NEW reference to access quantity and amount columns and
added validation to check quantity and amount values while inserting a new row.
Now we will try to insert invalid amount value in Product table using following statements.
The above statement returns Invalid Amount because we added validation to allow new row
insertion only when quantity and amount column values are greater than zero.
Now we will see what will happen when we try to insert invalid quantity value in
the Product table using the following statements.
The above statement also returns Invalid Quantity because we tried to insert value which is
less than zero.
Now we will try to insert valid data in the Product table using the following statements.
The phones and emails of the leads are so important that you can’t afford to lose this
information. For example, someone accidentally updates the email or phone to the wrong
ones or even delete it.
To protect this valuable data, you use a trigger to log all changes which are made to the
phone and email.
First, create a new table called product_logs to store the historical data.
CREATE TABLE Product_log
(pid INTEGER,
operation TEXT,
old_amount REAL,
new_amount REAL,
old_pname TEXT,
new_pname TEXT);
Now, create an AFTER UPDATE trigger to log the data into the Product_logs table
whenever there is an update in the Product table amount or pname columns. By this, we can
keep track of product prices in the future also.
Now we will check the records of the product_log table using following statement.
Now, we will try to update pname to “pencil” in the Product table where pid is 1.
Once we update let’s check the records of product_log table using following statement.
This is how we can use AFTER UPDATE statement to raise the trigger after updating table
columns.
Now, let’s delete one record from Product table using following query.
Once we delete now check records of product_log table using following statement.
This is how we can use AFTER DELETE statement to raise trigger after deleting table data
To drop an existing trigger, you use the DROP TRIGGER statement as follows:
DROP TRIGGER [IF EXISTS] trigger_name;
In this syntax:
First, specify the name of the trigger that you want to drop after the DROP
TRIGGER keywords.
Second, use the IF EXISTS option to delete the trigger only if it exists.
1. Enabled
An enabled trigger executes its trigger body if a triggering statement is issued and the trigger
restriction, if any, evaluates to true. By default, triggers are enabled when first created.
Only trigger:
ALTER TRIGGER validate_amount ENABLE;
All trigger on table:
ALTER TABLE product
ENABLE ALL TRIGGERS;
2. Disabled
A disabled trigger does not execute its trigger body, even if a triggering statement is issued
and the trigger restriction (if any) evaluates to true.
Only trigger:
ALTER TRIGGER validate_amount DISABLE;
All trigger on table:
ALTER TABLE product
DISABLE ALL TRIGGERS;
SQLite project delivers the sqlite3 tool that allows you to interact with the SQLite database
using the command-line program.
By using the sqlite3 tool, you can use the SQL statements to query or update data in the
database. In addition, you can use special commands, which are known as dot-commands to
perform various useful database operations.
One of these dot-commands is the .dump command that gives you the ability to dump the
entire database or tables into a text file.
2.1.1 Dump specific table into file, Dump only table structure
• From now on, every SELECT statement will issue the result as the
INSERT statements instead of pure text data.
• Second, set the output to a text file instead of the default standard output.
• Third, issue the SELECT statements to query data from a table that you want to
dump.
Steps:
1. Create .csv file or use exist file.
2. Change mode to csv by .mode command
3. Import it into sqlite database by command .import
Command:
sqlite>.mode CSV
Sqlite> .import <CSVFile> <TableName>
Example:
.mode CSV
.import username.csv usertable
(1) Table " usertable " does not previously exist and (2) table " usertable " does already exist.
In the first case, when the table does not previously exist, the table is automatically created
and the content of the first row of the input CSV file is used to determine the name of all the
columns in the table. In other words, if the table does not previously exist, the first row of the
CSV file is interpreted to be column names and the actual data starts on the second row of the
CSV file.
For the second case, when the table already exists, every row of the CSV file, including the
first row, is assumed to be actual content. If the CSV file contains an initial row of column
labels, you can cause the .import command to skip that initial row using the "--skip 1" option.
To export an SQLite table (or part of a table) as CSV, simply set the "mode" to "csv" and
then run a query to extract the desired rows of the table.
Steps:
Command:
sqlite>.mode csv
sqlite>.output stud.csv
Output:
Command Description