Python SQliteModule
Python SQliteModule
What is SQLite?
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. It is a software library that implements a
self-contained, serverless, zero-configuration, transactional SQL database
engine.
Serverless
Normally, an RDBMS such as MySQL, PostgreSQL, etc., requires a separate
server process to operate. The applications that want to access the database server
use TCP/IP protocol to send and receive requests. This is called client/server
architecture.
Self-Contained
SQLite is self-contained means it requires minimal support from the operating
system or external library. This makes SQLite usable in any environment
especially in embedded devices like iPhones, Android phones, game consoles,
handheld media players, etc.
SQLite is developed using ANSI-C. The source code is available as a big
sqlite3.c and its header file sqlite3.h. If you want to develop an application that
uses SQLite, you just need to drop these files into your project and compile it
with your code.
Zero-configuration
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.
In addition, SQLite does not use any configuration files.
Transactional
All transactions in SQLite are fully ACID-compliant. It means all queries and
changes are Atomic, Consistent, Isolated, and Durable,
ACID is a set of principles that ensure database transactions are processed
reliably. When any data storage system upholds those principles, it is said to be
ACID compliant.
In other words, all changes within a transaction take place completely or not at
all even when an unexpected situation like application crash, power failure, or
operating system crash occurs.
SQLite distinctive features
SQLite uses dynamic types for tables. It means you can store any value in any
column, regardless of the data type.
SQLite allows a single database connection to access multiple database files
simultaneously. This brings many nice features like joining tables in different
databases or copying data between databases in a single command.
SQLite is capable of creating in-memory databases that are very fast to work with.
Download SQLite tools
To download SQLite, you open the download page of the SQlite official website.
First, go to the https://www.sqlite.org website.
Second, open the download page https://www.sqlite.org/download.html
SQLite provides various tools for working across platforms e.g., Windows,
Linux, and Mac. You need to select an appropriate version to download.
Boolean Datatype
SQLite does not have a separate Boolean storage class. Instead, Boolean values
are stored as integers 0 (false) and 1 (true).
Date and Time Datatype
SQLite does not have a separate storage class for storing dates and/or times, but
SQLite is capable of storing dates and times as TEXT, REAL or INTEGER
values.
Sr.No. Storage Class & Date Format
1 TEXT
A date in a format like "YYYY-MM-DD HH:MM:SS.SSS"
2 REAL
The number of days since noon in Greenwich on November 24, 4714
B.C.
3 INTEGER
The number of seconds since 1970-01-01 00:00:00 UTC
SQLite CREATE TABLE statement
To create a new table in SQLite, you use CREATE TABLE statement using the
following syntax:
CREATE TABLE [IF NOT EXISTS] [schema_name].table_name (
column_1 data_type PRIMARY KEY,
column_2 data_type NOT NULL,
column_3 data_type DEFAULT 0,
table_constraints
) [WITHOUT ROWID];
✓ First, specify the name of the table that you want to create after the
CREATE TABLE keywords. The name of the table cannot start with
sqlite_ because it is reserved for the internal use of SQLite.
✓ Second, use IF NOT EXISTS option to create a new table if it does not
exist. Attempting to create a table that already exists without using the IF
NOT EXISTS option will result in an error.
✓ Third, optionally specify the schema_name to which the new table belongs.
The schema can be the main database, temp database or any attached
database.
✓ Fourth, specify the column list of the table. Each column has a name, data
type, and the column constraint. SQLite supports PRIMARY KEY,
UNIQUE, NOT NULL, and CHECK column constraints.
Notice that if you assign another integer type such as BIGINT and
UNSIGNED INT to the primary key column, this column will not be an alias
for the rowid column.
Because the rowid table organizes its data as a B-tree, querying and sorting
data of a rowid table are very fast. It is faster than using a primary key which
is not an alias of the rowid.
Another important note is that if you declare a column with the INTEGER
type and PRIMARY KEY DESC clause, this column will not become an alias
for the rowid column:
CREATE TABLE table(
Column1 INTEGER PRIMARY KEY DESC,
...
);
SQLite foreign key constraint
SQLite has supported foreign key constraint since version 3.6.19.
To check whether your current version of SQLite supports foreign key
constraints or not, you use the following command.
PRAGMA foreign_keys;
The command returns an integer value: 1: enable, 0: disabled. If the command
returns nothing, it means that your SQLite version doesn’t support foreign key
constraints.
If the SQLite library is compiled with foreign key constraint support, the
application can use the PRAGMA foreign_keys command to enable or disable
foreign key constraints at runtime.
SET NULL
When the parent key changes, delete or update, the corresponding child keys of
all rows in the child table set to NULL.
SET DEFAULT
The SET DEFAULT action sets the value of the foreign key to the default value
specified in the column definition when you create the table.
RESTRICT
The RESTRICT action does not allow you to change or delete values in the parent
key of the parent table.
NO ACTION
The NO ACTION does not mean by-pass the foreign key constraint. It has the
similar effect as the RESTRICT.
CASCADE
The CASCADE action propagates the changes from the parent table to the child
table when you update or delete the parent key.
Introduction to SQLite NOT NULL constraint
When you create a table, you can specify whether a column accepts NULL values
or not. By default, all columns in a table accept NULL values except you
explicitly use NOT NULL constraints.
To define a NOT NULL constraint for a column, you use the following syntax:
CREATE TABLE table_name (
...,
column_name type_name NOT NULL,
...
);
Unlike other constraints such as PRIMARY KEY and CHECK, you can only
define NOT NULL constraints at the column level, not the table level.
Based on the SQL standard, PRIMARY KEY should always imply NOT NULL.
However, SQLite allows NULL values in the PRIMARY KEY column except
that a column is INTEGER PRIMARY KEY column or the table is a WITHOUT
ROWID table or the column is defined as a NOT NULL column.
Once a NOT NULL constraint is attached to a column, any attempt to set the
column value to NULL such as inserting or updating will cause a constraint
violation.
Introduction to SQLite UNIQUE constraint
A UNIQUE constraint ensures all values in a column or a group of columns are
distinct from one another or unique.
To define a UNIQUE constraint, you use the UNIQUE keyword followed by one
or more columns.
You can define a UNIQUE constraint at the column or the table level. Only at the
table level, you can define a UNIQUE constraint across multiple columns.
The following shows how to define a UNIQUE constraint for a column at the
column level:
CREATE TABLE table_name(
...,
column_name type UNIQUE,
...
);
The following illustrates how to define a UNIQUE constraint for multiple
columns:
CREATE TABLE table_name(
...,
UNIQUE(column_name1,column_name2,...)
);
If the values do not meet the criteria defined by the expression, SQLite will issue
a constraint violation and abort the statement.
The CHECK constraints allow you to define additional data integrity checks
beyond UNIQUE or NOT NULL to suit your specific application.
SQLite allows you to define a CHECK constraint at the column level or the table
level.
The following statement shows how to define a CHECK constraint at the column
level:
CREATE TABLE table_name(
...,
column_name data_type CHECK(expression),
...
);
and the following statement illustrates how to define a CHECK constraint at the
table level:
CREATE TABLE table_name(
...,
CHECK(expression)
);
In this syntax, whenever a row is inserted into a table or an existing row is
updated, the expression associated with each CHECK constraint is evaluated and
returned a numeric value 0 or 1.
If the result is zero, then a constraint violation occurred. If the result is a non-zero
value or NULL, it means no constraint violation occurred.
If you don’t specify the rowid value or you use a NULL value when you insert a
new row, SQLite automatically assigns the next sequential integer, which is one
larger than the largest rowid in the table. The rowid value starts at 1.
The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and
disk I/O overhead and should be avoided if not strictly needed. It is usually not
needed.
In addition, the way SQLite assigns a value for the AUTOINCREMENT column
slightly different from the way it does for the rowid column.
The main purpose of using attribute AUTOINCREMENT is to prevent SQLite to
reuse a value that has not been used or a value from the previously deleted row.
If you don’t have any requirement like this, you should not use the
AUTOINCREMENT attribute in the primary key.
✓ First, specify the name of the table to which you want to insert data after
the INSERT INTO keywords.
✓ Second, add a comma-separated list of columns after the table name. The
column list is optional. However, it is a good practice to include the column
list after the table name.
✓ Third, add a comma-separated list of values after the VALUES keyword.
If you omit the column list, you have to specify values for all columns in
the value list. The number of values in the value list must be the same as
the number of columns in the column list.
SQLite INSERT – Inserting multiple rows into a table
SQLite Update
To update existing data in a table, you use SQLite UPDATE statement. The
following illustrates the syntax of the UPDATE statement:
UPDATE table
SET column_1 = new_value_1,
column_2 = new_value_2
WHERE
search_condition
ORDER column_or_expression
LIMIT row_count OFFSET offset;
SQLite Delete
The SQLite DELETE statement allows you to delete one row, multiple rows, and
all rows in a table. The syntax of the SQLite DELETE statement is as follows:
simplest form of the SELECT statement that allows you to query data from a
single table.
Even though the SELECT clause appears before the FROM clause, SQLite
evaluates the FROM clause first and then the SELECT clause, therefore:
First, specify the table where you want to get data from in the FROM clause.
Notice that you can have more than one table in the FROM clause.
SQLite stores data in the tables in an unspecified order. It means that the rows in
the table may or may not be in the order that they were inserted.
If you use the SELECT statement to query data from a table, the order of rows
in the result set is unspecified.
To sort the result set, you add the ORDER BY clause to the SELECT statement
as follows:
SELECT
select_list
FROM
table
ORDER BY
column_1 ASC,
column_2 DESC;
The ORDER BY clause comes after the FROM clause. It allows you to sort the
result set based on one or more columns in ascending or descending order.
In this syntax, you place the column name by which you want to sort after the
ORDER BY clause followed by the ASC or DESC keyword.
In case you want to sort the result set by multiple columns, you use a comma (,)
to separate two columns. The ORDER BY clause sorts rows using columns or
expressions from left to right.
SQLite considers NULL values as duplicates. If you use the DISTINCT clause
with a column that has NULL values, SQLite will keep one row of a NULL value.
SQLite Where
The WHERE clause is an optional clause of the SELECT statement. It appears
after the FROM clause as the following statement:
SELECT
column_list
FROM
table
WHERE
search_condition;
Notice that SQLite does not provide Boolean data type therefore 1 means TRUE,
and 0 means FALSE.
Operator Meaning
ALL returns 1 if all expressions are 1.
AND returns 1 if both expressions are 1, and 0 if one of the expressions
is 0.
ANY returns 1 if any one of a set of comparisons is 1.
BETWEEN returns 1 if a value is within a range.
EXISTS returns 1 if a subquery contains any rows.
IN returns 1 if a value is in a list of values.
LIKE returns 1 if a value matches a pattern
NOT reverses the value of other operators such as NOT EXISTS, NOT
IN, NOT BETWEEN, etc.
SQLite 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.
For example, a SELECT statement may return one million rows. However, if you
just need the first 10 rows in the result set, you can add the LIMIT clause to the
SELECT statement to retrieve 10 rows.
The following illustrates the syntax of the LIMIT clause.
SELECT
column_list
FROM
table
LIMIT row_count;
The row_count is a positive integer that specifies the number of rows returned.
If you want to get the first 10 rows starting from the 10th row of the result set,
you use OFFSET keyword as the following:
SELECT
column_list
FROM
table
LIMIT row_count OFFSET offset;
Or you can use the following shorthand syntax of the LIMIT OFFSET clause:
SELECT
column_list
FROM
table
LIMIT offset, row_count;
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 following error:
SELECT
column_1,
column_2,
aggregate_function (column_3)
FROM
table
GROUP BY
column_1,
column_2
HAVING
search_condition;
In this syntax, the HAVING clause evaluates the search_condition for each group
as a Boolean expression. It only includes a group in the final result set if the
evaluation is true.
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. The following illustrates the basic syntax of the UNION
operator:
query_1
UNION [ALL]
query_2
UNION [ALL]
query_3
...;
Both UNION and UNION ALL operators combine rows from result sets into a
single result set. The UNION operator removes eliminate duplicate rows, whereas
the UNION ALL operator does not.
Because the UNION ALL operator does not remove duplicate rows, it runs faster
than the UNION operator.
The basic rules for combining the result sets of two queries are as follows:
✓ First, the number and the order of the columns in all queries must be the
same.
✓ Second, the data types must be comparable.
Introduction to SQLite subquery
A subquery is a SELECT statement nested in another statement. See the following
statement.
SELECT column_1
FROM table_1
WHERE column_1 = (
SELECT column_1
FROM table_2
);
You must use a pair of parentheses to enclose a subquery. Note that you can nest
a subquery inside another subquery with a certain depth.
You can use a subquery in the SELECT, FROM, WHERE, and JOIN clauses.
In this syntax, the subquery is a SELECT statement that returns zero or more
rows.
If the subquery returns one or more row, the EXISTS operator return true.
Otherwise, the EXISTS operator returns false or NULL.
Note that if the subquery returns one row with NULL, the result of the EXISTS
operator is still true because the result set contains one row with NULL.
To negate the EXISTS operator, you use the NOT EXISTS operator as follows:
NOT EXISTS (subquery)
The NOT EXISTS operator returns true if the subquery returns no row.
SQLite Join
In SQLite, JOIN clause is used to combine records from two or more tables in a
database. It unites fields from two tables by using the common values of the both
table.
LEFT JOIN
This type of join returns all rows from the LEFT-hand table specified in the ON
condition and only those rows from the other table where the joined fields are
equal (join condition is met).
Syntax
SELECT columns FROM table1 LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
the LEFT OUTER JOIN keywords are replaced with LEFT JOIN.
SQLite CASE
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.
Otherwise, the simple CASE expression compares the case_expression with the
expression in the next WHEN clause.
Unlike the LIKE operator, the GLOB operator is case sensitive and uses the
UNIX wildcards. In addition, the GLOB patterns do not have escape characters.
The following shows the wildcards used with the GLOB operator:
✓ The asterisk (*) wildcard matches any number of characters.
✓ The question mark (?) wildcard matches exactly one character.
On top of these wildcards, you can use the list wildcard [] to match one
character from a list of characters. For example [xyz] match any single x, y, or
z character.
The list wildcard also allows a range of characters e.g., [a-z] matches any single
lowercase character from a to z. The [a-zA-Z0-9] pattern matches any single
alphanumeric character, both lowercase, and uppercase.
Besides, you can use the character ^ at the beginning of the list to match any
character except for any character in the list. For example, the [^0-9] pattern
matches any single character except a numeric character.
Syntax
SELECT Columns FROM Table WHERE Column GLOB 'pattern';
SQLite Transaction
SQLite & ACID
SQLite is a transactional database that all changes and queries are atomic,
consistent, isolated, and durable (ACID).
SQLite guarantees all the transactions are ACID compliant even if the transaction
is interrupted by a program crash, operation system dump, or power failure to the
computer.
Atomic: a transaction should be atomic. It means that a change cannot be broken
down into smaller ones. When you commit a transaction, either the entire
transaction is applied or not.
Consistent: a transaction must ensure to change the database from one valid state
to another. When a transaction starts and executes a statement to modify data, the
database becomes inconsistent. However, when the transaction is committed or
rolled back, it is important that the transaction must keep the database consistent.
Isolation: a pending transaction performed by a session must be isolated from
other sessions. When a session starts a transaction and executes the INSERT or
UPDATE statement to change the data, these changes are only visible to the
current session, not others. On the other hand, the changes committed by other
sessions after the transaction started should not be visible to the current session.
Durable: if a transaction is successfully committed, the changes must be
permanent in the database regardless of the condition such as power failure or
program crash. On the conflicting, if the program crashes before the transaction
is committed, the change should not persist.
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;
SQLite Trigger
An SQLite trigger is a named database object that is executed automatically when
an INSERT, UPDATE or DELETE statement is issued against the associated
table.
✓ 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.
Notice that if you drop a table, all associated triggers are also deleted.
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.
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.
Note that if you drop a table, SQLite will automatically drop all triggers
associated with the table.
Using the TEXT storage class for storing SQLite date and time
If you use the TEXT storage class to store date and time value, you need to use
the ISO8601 string format as follows:
YYYY-MM-DD HH:MM:SS.SSS
To insert date and time values into the table, you use the DATETIME function.
For example, to get the current UTC date and time value, you pass the now literal
string to the function as follows:
SELECT datetime('now');
Using REAL storage class to store SQLite date and time values
You can use the REAL storage class to store the date and/ or time values as Julian
day numbers, which is the number of days since noon in Greenwich on November
24, 4714 B.C. based on the proleptic Gregorian calendar.
We used the julianday() function to convert the current date and time to the
Julian Day.
julianday('now')
Using INTEGER to store SQLite date and time values
Besides TEXT and REAL storage classes, you can use the INTEGER storage
class to store date and time values.
We typically use the INTEGER to store UNIX time which is the number of
seconds since 1970-01-01 00:00:00 UTC
strftime('%s','now')
Unit-2: Database backup and CSV handling:
Dump the entire database into a file using the SQLite dump command
To dump a database into a file, you use the .dump command. The .dump
command converts the entire structure and data of an SQLite database into a
single text file.
sqlite3 file-path
.dump
.exit
To dump a specific table, you specify the table name after the .dump command.
.output file-path
.dump tablename
.exit
The simplicity of CSV means that it is universally readable by any program you
use - such as Excel, Numbers, your CRM or other databases. So, regardless of
where you export or import data from, CSV will work.
Another benefit, (not to be discounted!) is the fact that CSV is data stored exactly
"as-is". That means that what data you have saved will never be wrongly
formatted or translated, regardless of where you open it or import it into.
Backing up Data
Data backups are an essential business process, and so it is important to ensure
your data is being saved in a format that is both readable and accessible to you.
CSV, being the universal language, will ensure that the data you backup is able
to be opened and read, and even imported back into your database.
e.g.
sqlite> .header on
sqlite> .mode csv
sqlite> .output Employee.csv
sqlite> SELECT * FROM TABLE;
sqlite> .quit
✓ To insert table column names in CSV or Excel file we used .header on
command.
✓ To return the data in CSV format we used .mode CSV.
✓ To send data to CSV file we used .output command and SELECT statement
to export data from the required table.
Import a CSV File into an SQLite Table
If you want to import data from CSV file into a table that does not exist in the
SQLite database.
To import the csv file into the table:
First, set the mode to CSV to instruct the command-line shell program to
interpret the input file as a CSV file.
To do this, you use the .mode command as follows:
sqlite> .mode csv
Second, use the command .import FILE TABLE to import the data from the
city.csv file into the cities table.
We will use the SQLite Studio to show you how to import a CSV file into a
table with the assumption that the target table already exists in the database.
Unit-3: Python interaction with SQLite
Python Module
A module is a file containing Python definitions and statements. A module can
define functions, classes and variables. A module can also include runnable code.
Grouping related code into a module makes the code easier to understand and use.
Simply, a module is a file consisting of Python code.
e.g.
The Python code for a module named “myModule” normally resides in a file
named “myModule.py”.
e.g.
def print_func( par ):
print ("Hello : ", par)
return
save this code in myModule.py file
import statement
You can use any Python source file as a module by executing an import statement
in some other Python source file.
Syntax:
import module1[, module2[,... moduleN]
When the interpreter encounters an import statement, it imports the module if the
module is present in the search path. A search path is a list of directories that the
interpreter searches before importing a module.
e.g.
import myModule
myModule.fuc(“SYBCA”)
When you import a module, the Python interpreter searches for the module in
the following sequences −
1. The current directory.
2. If the module isn't found, Python then searches each directory in the shell
variable PYTHONPATH.
3. If all else fails, Python checks the default path.
The module search path is stored in the system module sys as the sys.path
variable. The sys.path variable contains the current directory, PYTHONPATH,
and the installation-dependent default.
Each function has its own local namespace. Class methods follow the same
scoping rule as ordinary functions.
Python makes educated guesses on whether variables are local or global. It
assumes that any variable assigned a value in a function is local.
Therefore, in order to assign a value to a global variable within a function, you
must first use the global statement.
The statement global VarName tells Python that VarName is a global variable.
Python stops searching the local namespace for the variable.
e.g.
Money = 2000
def AddMoney():
# Uncomment the following line to fix the code:
# global Money
Money = Money + 1
print Money
AddMoney()
print (Money)
Python Package
Python has packages for directories and modules for files. As our application
program grows larger in size with a lot of modules, we place similar modules in
one package and different modules in different packages. This makes a project
(program) easy to manage and conceptually clear.
A Python module may contain several classes, functions, variables, etc. whereas
a Python package can contain several modules. In simpler terms a package is
folder that contains various modules as files.
Creating Package
✓ Create a folder named with packagename.
✓ Inside this folder create an empty Python file i.e. __init__.py
✓ Then create two modules in that folder.
__init__.py
__init__.py helps the Python interpreter to recognise the folder as package. It also
specifies the resources to be imported from the modules. If the __init__.py is
empty this means that all the functions of the modules will be imported.
We can also specify the functions from each module to be made available.
Syntax
from .modulename import function,class,variable
Syntax
import package_name.module_name
try:
# do something
pass
except ValueError:
# handle ValueError exception
pass
except (TypeError, ZeroDivisionError):
# handle multiple exceptions
# TypeError and ZeroDivisionError
pass
except:
# handle all other exceptions
pass
Syntax:
try:
# Some Code....
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)
Raising Exception
In Python programming, exceptions are raised when errors occur at runtime. We
can also manually raise exceptions using the raise keyword. This must be either
an exception instance or an exception class (a class that derives from Exception).
try:
raise NameError("Hi there") # Raise Error
except NameError:
print ("An exception")
raise # To determine whether the exception was raised or not
sqlite3 Module in Python
SQLite is a C library that provides a lightweight disk-based database that doesn’t
require a separate server process and allows accessing the database using a
nonstandard variant of the SQL query language.
The sqlite3 module was written by Gerhard Häring.
import sqlite3
con = sqlite3.connect('databasefile')
Once a Connection has been established, create a Cursor object and call its
execute() method to perform SQL commands:
Syntax:
execute(sql, parameters=(), /)
Execute an SQL statement. Values may be bound to the statement using
placeholders.
execute() will only execute a single SQL statement. If you try to execute more
than one statement with it, it will raise a Warning. Use executescript() if you
want to execute multiple SQL statements with one call.
To insert a variable into a query string, use a placeholder in the string, and
substitute the actual values into the query by providing them as a tuple of values
to the second argument of the cursor’s execute() method. An SQL statement may
use one of two kinds of placeholders: question marks (qmark style) or named
placeholders (named style).
For the qmark style, parameters must be a sequence.
For the named style, it can be either a sequence or dict instance.
executemany(sql, seq_of_parameters, /)
Execute a parameterized SQL command against all parameter sequences or
mappings found in the sequence seq_of_parameters. It is also possible to use an
iterator yielding parameters instead of a sequence.
executescript(sql_script, /)
Execute multiple SQL statements at once. If there is a pending transaciton, an
implicit COMMIT statement is executed first. No other implicit transaction
control is performed; any transaction control must be added to sql_script.
sql_script must be a string.
fetchone()
The fetchone() returns the next row of a query result set, returning a single tuple,
or None when no more data is available.
e.g.
import sqlite3
con = sqlite3.connect('ydb.db')
with con:
cur = con.cursor()
cur.execute("SELECT * FROM cars")
while True:
row = cur.fetchone()
if row == None:
break
print(f"{row[0]} {row[1]} {row[2]}")
Python treats file differently as text or binary and this is important. Each line of
code includes a sequence of characters and they form text file. Each line of a file
is terminated with a special character, called the EOL or End of Line characters
like comma {,} or newline character. It ends the current line and tells the
interpreter a new one has begun.
When we want to read from or write to a file, we need to open it first. When we
are done, it needs to be closed so that the resources that are tied with the file are
freed.
Hence, in Python, a file operation takes place in the following order:
1. Open a file
2. Read or write (perform operation)
3. Close the file
Opening Files in Python
Python has a built-in open() function to open a file. This function returns a file
object, also called a handle, as it is used to read or modify the file accordingly.
In addition, you can specify if the file should be handled as binary or text mode
"t" - Text - Default value. Text mode
"b" - Binary - Binary mode (e.g. images)
e.g.
f = open("test.txt") # equivalent to 'r' or 'rt'
f = open("test.txt",'w') # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode
tell()
Returns an integer that represents the current position of the file's object.
seek()
Changes the file position to offset bytes, in reference to from (start, current, end).
Syntax:
Syntax: fseek(offset, from_what)
Parameters:
Offset: Number of positions to move forward
from_what: It defines point of reference.
Returns: Return the new absolute position.
The reference point is selected by the from_what argument.
It accepts three values:
0: sets the reference point at the beginning of the file
1: sets the reference point at the current file position
2: sets the reference point at the end of the file
By looping through the lines of the file, you can read the whole file, line by line:
f = open("demofile.txt", "r")
for x in f:
print(x)
readline()
The readlines() method returns a list of remaining lines of the entire file. All these
reading methods return empty values when the end of file (EOF) is reached.
write()
Writes the string s to the file and returns the number of characters written.
Syntax: write(string)
writelines()
Writes a list of lines to the file.
Syntax: writelines(lines)
Delete a File
To delete a file, you must import the OS module, and run
its os.remove() function:
e.g.
Remove the file "demofile.txt":
import os
os.remove("demofile.txt")
DictReader
It creates an object which maps the information read into a dictionary whose keys
are given by the fieldnames parameter. This parameter is optional, but when not
specified in the file, the first row data becomes the keys of the dictionary.
e.g.
import csv
with open('name.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['first_name'], row['last_name'])
DictWriter
This class is similar to the DictWriter class and does the opposite, which is writing
data to a CSV file. The class is defined as
csv.DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel',
*args, **kwds)
The fieldnames parameter defines the sequence of keys that identify the order in
which values in the dictionary are written to the CSV file.
Unlike the DictReader, this key is not optional and must be defined in order to
avoid errors when writing to a CSV.
e.g.
import csv
csvfile = open('names.csv', 'w', newline='')
fieldnames = ['first_name', 'last_name']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
The Pandas DataFrame
The Pandas DataFrame is a structure that contains two-dimensional data and its
corresponding labels. DataFrames are widely used in data science, machine
learning, scientific computing, and many other data-intensive fields.
DataFrames are similar to SQL tables or the spreadsheets that you work with in
Excel or Calc.
In the real world, a Pandas DataFrame will be created by loading the datasets
from existing storage, storage can be SQL Database, CSV file, and Excel file.
Pandas DataFrame can be created from the lists, dictionary, and from a list of
dictionary etc.
# list of strings
lst = ['kbs', 'College', 'Vapi']
# Calling DataFrame constructor on list
df = pd.DataFrame(lst)
print(df)
Dataframe using list with index and column names
# import pandas as pd
import pandas as pd
# list of strings
lst = ['Valsad', 'Surat', 'Vapi']
# list of strings
lst = ['Pritesh', 'Nimesh', 'Mitesh']
# list of int
lst2 = [101, 102, 103]
# dictionary of lists
dict = {'name': nme, 'degree': deg, 'score': scr}
df = pd.DataFrame(dict)
print(df)