0% found this document useful (0 votes)
8 views14 pages

Unit 3

Unit 3 covers Python interaction with SQLite, focusing on creating and using modules, packages, and the sqlite3 module for database operations. It explains how to create modules, import them, manage namespaces, and utilize the sqlite3 module to connect to and manipulate SQLite databases. Additionally, it includes examples of creating tables and performing CRUD operations using SQL statements in Python.

Uploaded by

netrak1707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

Unit 3

Unit 3 covers Python interaction with SQLite, focusing on creating and using modules, packages, and the sqlite3 module for database operations. It explains how to create modules, import them, manage namespaces, and utilize the sqlite3 module to connect to and manipulate SQLite databases. Additionally, it includes examples of creating tables and performing CRUD operations using SQL statements in Python.

Uploaded by

netrak1707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Unit: 3 Python interaction with SQLite

Unit-3: Python interaction with SQLite:

Unit 3.1 Module: Concepts of module and Using modules in python

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. It also makes the code logically organized. A module is a Python object
with arbitrarily named attributes that you can bind and reference.
Simply, a module is a file consisting of Python code. A module can define functions, classes and variables.
A module can also include runnable code.
Create a Module

To create a module just save the code you want in a file with the file extension .py:

Example

Save this code in a file named mymodule.py

def greeting(name):
print("Hello, " + name)

Use a Module/import statement

You can use any Python source file as a module by executing an import statement in some other Python
source file. The import has the following 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.

Now we can use the module we just created, by using the import statement:

import mymodule

mymodule.greeting("Jonathan")

A module is loaded only once, regardless of the number of times it is imported. This prevents the module
execution from happening over and over again if multiple imports occur.

Variables in Module

The module can contain functions, as already described, but also variables of all types (arrays, dictionaries,
objects etc):

Prepared by: Shreya Patel, CBPCC


Unit: 3 Python interaction with SQLite

Example

Save this code in the file mymodule.py

person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}

Import the module named mymodule, and access the person1 dictionary:

import mymodule

a = mymodule.person1["age"]
print(a)

Naming a Module

You can name the module file whatever you like, but it must have the file extension .py

Re-naming a Module

You can create an alias when you import a module, by using the as keyword:

import mymodule as mx

Example

Create an alias for mymodule called mx:

a = mx.person1["age"]
print(a)

The from...import Statement

Prepared by: Shreya Patel, CBPCC


Unit: 3 Python interaction with SQLite

Python's from statement lets you import specific attributes from a module into the current namespace.
The from...import has the following syntax −
from modname import name1[, name2[, ... nameN]]
Example

The module named mymodule has one function and one dictionary:

def greeting(name):
print("Hello, " + name)

person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}

Import only the person1 dictionary from the module:

from mymodule import person1

print (person1["age"])
This statement does not import the entire module mymodule into the current namespace; it just introduces
the item person1 from the module mymodule into the global symbol table of the importing module.

The from...import * Statement

It is also possible to import all names from a module into the current namespace by using the following
import statement −
from modname import *
This provides an easy way to import all the items from a module into the current namespace.
Unit 3.1.1 Setting PYTHONPATH, Concepts of Namespace and Scope

Locating Modules

When you import a module, the Python interpreter searches for the module in the following sequences −
• The current directory.
• If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.
• If all else fails, Python checks the default path. On UNIX, this default path is normally
/usr/local/lib/python/.
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.

The PYTHONPATH Variable

The PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of


PYTHONPATH is the same as that of the shell variable PATH.
Here is a typical PYTHONPATH from a Windows system −

Prepared by: Shreya Patel, CBPCC


Unit: 3 Python interaction with SQLite

set PYTHONPATH = c:\python20\lib;

Namespaces and Scoping

Variables are names (identifiers) that map to objects. A namespace is a dictionary of variable names (keys)
and their corresponding objects (values).
A Python statement can access variables in a local namespace and in the global namespace. If a local and a
global variable have the same name, the local variable shadows the global variable.
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.
For example, we define a variable Money in the global namespace. Within the function Money, we
assign Money a value, therefore Python assumes Money as a local variable. However, we accessed the value
of the local variable Money before setting it, so an UnboundLocalError is the result. Uncommenting the
global statement fixes the problem.
Money = 2000
def AddMoney():
# Uncomment the following line to fix the code:
# global Money
Money = Money + 1

print Money
AddMoney()
print Money

The dir( ) Function

The dir() built-in function returns a sorted list of strings containing the names defined by a module.
The list contains the names of all the modules, variables and functions that are defined in a module. Following
is a simple example –

import math

content = dir(math)

print content

When the above code is executed, it produces the following result −

['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan',


'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp',
'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh',
Prepared by: Shreya Patel, CBPCC
Unit: 3 Python interaction with SQLite

'sqrt', 'tan', 'tanh']


Here, the special string variable __name__ is the module's name, and __file__ is the filename from which the
module was loaded.

The globals() and locals() Functions

The globals() and locals() functions can be used to return the names in the global and local namespaces
depending on the location from where they are called.
If locals() is called from within a function, it will return all the names that can be accessed locally from that
function.
If globals() is called from within a function, it will return all the names that can be accessed globally from
that function.
The return type of both these functions is dictionary. Therefore, names can be extracted using the keys()
function.

The reload() Function

When the module is imported into a script, the code in the top-level portion of a module is executed only
once.
Therefore, if you want to reexecute the top-level code in a module, you can use the reload() function. The
reload() function imports a previously imported module again. The syntax of the reload() function is this −
reload(module_name)
Here, module_name is the name of the module you want to reload and not the string containing the module
name. For example, to reload hello module, do the following –
reload(hello)

Unit 3.1.2 Concepts of Packages in python


We organize a large number of files in different folders and subfolders based on some criteria, so that
we can find and manage them easily. In the same way, a package in Python takes the concept of the
modular approach to next logical level. As you know, a module can contain multiple objects, such as
classes, functions, etc. A package can contain one or more relevant modules. Physically, a package is
actually a folder containing one or more module files. A package is a hierarchical file directory structure
that defines a single Python application environment that consists of modules and subpackages and sub-
subpackages, and so on.
Let's create a package named mypackage, using the following steps:

• Create a new folder named D:\MyApp.


• Inside MyApp, create a subfolder with the name 'mypackage'.
• Create an empty __init__.py file in the mypackage folder.
• Using a Python-aware editor like IDLE, create modules greet.py and functions.py with the following
code:

Greet.py
def SayHello(name):
print("Hello ", name)

function.py

Prepared by: Shreya Patel, CBPCC


Unit: 3 Python interaction with SQLite

def sum(x,y):
return x+y

def average(x,y):
return (x+y)/2

def power(x,y):
return x**y

That's it. We have created our package called mypackage. The following is a folder structure:

Importing a Module from a Package


Now, to test our package, navigate the command prompt to the MyApp folder and invoke the Python
prompt from there.
D:\MyApp>python
Import the functions module from the mypackage package and call its power() function.
>>>from mypackage import functions
>>>functions.power(3,2)
9
It is also possible to import specific functions from a module in the package.
>>>from mypackage.functions import sum
>>>sum(10,20)
30
>>>average(10,12)
Traceback (most recent call last):
File <”pyshell#13”, line 1 in <module>
NameError: name ‘average’ is not defined

__init__.py

The package folder contains a special file called __init__.py, which stores the package's content. It serves
two purposes:

1. The Python interpreter recognizes a folder as the package if it contains __init__.py file.
Prepared by: Shreya Patel, CBPCC
Unit: 3 Python interaction with SQLite

2. __init__.py exposes specified resources from its modules to be imported.

An empty __init__.py file makes all functions from the above modules available when this package is
imported. Note that __init__.py is essential for the folder to be recognized by Python as a package. You
can optionally define functions from individual modules to be made available.

The __init__.py file is normally kept empty. However, it can also be used to choose specific functions
from modules in the package folder and make them available for import. Modify __init__.py as below:

__init__.py

from .functions import average, power


from .greet import SayHello

The specified functions can now be imported in the interpreter session or another executable script.

Create test.py in the MyApp folder to test mypackage.

test.py

from mypackage import power, average, SayHello


SayHello()
x=power(3,2)
print("power(3,2) : ", x)

Note that functions power() and SayHello() are imported from the package and not from their respective
modules, as done earlier. The output of the above script is:

D:\MyApp>python test.py
Hello world
Power(3,2)
9

Install a Package Globally

Once a package is created, it can be installed for system-wide use by running the setup script. The script
calls setup() function from the setuptools module.

Let's install mypackage for system-wide use by running a setup script.

Save the following code as setup.py in the parent folder MyApp. The script calls the setup() function from
the setuptools module. The setup() function takes various arguments such as name, version, author, list of
dependencies, etc. The zip_safe argument defines whether the package is installed in compressed mode or
regular mode.

setup.py

from setuptools import setup


setup(name='mypackage',
version='0.1',
description='Testing installation of Package',

Prepared by: Shreya Patel, CBPCC


Unit: 3 Python interaction with SQLite

url='#',
author='auth',
author_email='[email protected]',
license='MIT',
packages=['mypackage'],
zip_safe=False)

Now execute the following command to install mypackage using the pip utility. Ensure that the command
prompt is in the parent folder, in this case D:\MyApp.

D:\MyApp>pip install mypackage


Processing d:\MyApp
Installing collected packages: mypack
Running setup.py install for mypack … done
Successfully installed mypackage – 0.1

Now mypackage is available for system-wide use and can be imported in any script or interpreter.

D:\>python
>>> import mypackage
>>>mypackage.average(10,20)
15.0
>>>mypackage.power(10,2)
100

Unit 3.2 Importing sqlite3 module

SQLite3 can be integrated with Python using sqlite3 module. Python sqlite3 module adheres to Python
Database API Specification, provides a SQL interface designed to encourage and maintain the similarity
between the Python modules used to access databases. You do not need to install this module separately
because it is shipped by default along with Python version 2.5.x onwards.
Use the following steps to connect to SQLite

How to Connect to SQLite Database in Python

Import sqlite3 module

import sqlite3 statement imports the sqlite3 module in the program. Using the classes and methods defined
in the sqlite3 module we can communicate with the SQLite database.

Unit 3.2.1 connect () and execute() methods.

Connect ()

Use the connect() method of the connector class with the database name. To establish a connection to
SQLite, you need to pass the database name you want to connect. If you specify the database file name that
already presents on the disk, it will connect to it. But if your specified SQLite database file doesn’t exist,
SQLite creates a new database for you. This method returns the SQLite Connection Object if the connection
is successful. You can specify filename with the required path as well if you want to create a database
anywhere else except in the current directory.

Connect To Database
Prepared by: Shreya Patel, CBPCC
Unit: 3 Python interaction with SQLite

Following Python code shows how to connect to an existing database. If the database does not exist, then it
will be created and finally a database object will be returned.
import sqlite3

conn = sqlite3.connect('test.db')

print "Opened database successfully";

You can change your path as per your requirement. Keep the above code in sqlite.py file and execute it as
shown below. If the database is successfully created, then it will display the following message

Open database successfully

execute()

The execute() methods run the SQL query and return the result. This routine executes an SQL statement.
The SQL statement may be parameterized.

Unit 3.2.3 Select, Insert, update, delete using execute () method

Create a Table

Following Python program will be used to create a table in the previously created database.
import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

conn.execute('''CREATE TABLE COMPANY


(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);''')
print "Table created successfully";

conn.close()

When the above program is executed, it will create the COMPANY table in your test.db and it will display
the following messages –

Opened database successfully


Table created successfully

INSERT Operation

Following Python program shows how to create records in the COMPANY table created in the above
example.
import sqlite3

Prepared by: Shreya Patel, CBPCC


Unit: 3 Python interaction with SQLite

conn = sqlite3.connect('test.db')
print "Opened database successfully";

conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \


VALUES (1, 'Paul', 32, 'California', 20000.00 )");

conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \


VALUES (2, 'Allen', 25, 'Texas', 15000.00 )");

conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \


VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )");

conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \


VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )");

conn.commit()
print "Records created successfully";
conn.close()

When the above program is executed, it will create the given records in the COMPANY table and it will
display the following two lines –

Opened database successfully


Records created successfully

SELECT Operation

Following Python program shows how to fetch and display records from the COMPANY table created in the
above example.
import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

c = conn.execute("SELECT id, name, address, salary from COMPANY")


for row in c:
print "ID = ", row[0]
print "NAME = ", row[1]
print "ADDRESS = ", row[2]
print "SALARY = ", row[3], "\n"

print "Operation done successfully";


conn.close()

When the above program is executed, it will produce the following result.

Opened database successfully


ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000.0

Prepared by: Shreya Patel, CBPCC


Unit: 3 Python interaction with SQLite

ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0

ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0

ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0

Operation done successfully

UPDATE Operation

Following Python code shows how to use UPDATE statement to update any record and then fetch and display
the updated records from the COMPANY table.
import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

conn.execute("UPDATE COMPANY set SALARY = 25000.00 where ID = 1")


conn.commit()
print "Total number of rows updated :", conn.total_changes

cursor = conn.execute("SELECT id, name, address, salary from COMPANY")


for row in cursor:
print "ID = ", row[0]
print "NAME = ", row[1]
print "ADDRESS = ", row[2]
print "SALARY = ", row[3], "\n"

print "Operation done successfully";


conn.close()

When the above program is executed, it will produce the following result.

Opened database successfully


Total number of rows updated : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000.0

ID = 2
Prepared by: Shreya Patel, CBPCC
Unit: 3 Python interaction with SQLite

NAME = Allen
ADDRESS = Texas
SALARY = 15000.0

ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0

ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0

Operation done successfully

DELETE Operation

Following Python code shows how to use DELETE statement to delete any record and then fetch and display
the remaining records from the COMPANY table.
import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

conn.execute("DELETE from COMPANY where ID = 2;")


conn.commit()
print "Total number of rows deleted :", conn.total_changes

cursor = conn.execute("SELECT id, name, address, salary from COMPANY")


for row in cursor:
print "ID = ", row[0]
print "NAME = ", row[1]
print "ADDRESS = ", row[2]
print "SALARY = ", row[3], "\n"

print "Operation done successfully";


conn.close()

When the above program is executed, it will produce the following result.
Opened database successfully
Total number of rows deleted : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000.0

Prepared by: Shreya Patel, CBPCC


Unit: 3 Python interaction with SQLite

ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0

ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0

Operation done successfully

Unit 3.2.4 commit () method


The commit() method is used to make sure the changes made to the database are consistent. It
basically provides the database confirmation regarding the changes made by a user or an
application in the database. This method commits the current transaction. If you don't call this method,
anything you did since the last call to commit() is not visible from other database connections.
Syntax:
Con.commit()
Unit 3.2.2 Single row and multi-row fetch (fetchone(), fetchall())
fetchall()
This routine fetches all rows of a query result, returning a list. An empty list is returned when no rows are
available. To fetch the data from a database, we will execute the SELECT statement and then will use
the fetchall() method of the cursor object to store the values into a variable. After that, we will loop through
the variable and print all values. The code will be like this:

import sqlite3

conn = sqlite3.connect('D:\SQLite3\sqlite3\item_dist.db')

print ("Database successfully created")

c = conn.cursor()

c.execute("SELECT id, name, address, salary from COMPANY")

rows = c.fetchall()

row in rows:

print(row)

#print(rows)

conn.close()

Prepared by: Shreya Patel, CBPCC


Unit: 3 Python interaction with SQLite

fetchone()
When you want to read only one row from the SQLite table, then you should use fetchone() method of a
cursor class. You can also use this method in situations when you know the query is going to return only one
row.

import sqlite3

conn = sqlite3.connect('D:\SQLite3\sqlite3\item_dist.db')

print ("Database successfully created")

c = conn.cursor()

c.execute("SELECT id, name, address, salary from COMPANY")

print(c.fetchone())

conn.close()

Prepared by: Shreya Patel, CBPCC

You might also like