PY Mod 4
PY Mod 4
Database Programming
Python being a high-level language provides support for various databases. We can
connect and run queries for a particular database using Python and without writing
raw queries in the terminal or shell of that particular database, we just need to have
that database installed in our system.
The Python programming language has powerful features for database programming.
Python supports various databases like SQLite, MySQL, Oracle, Sybase, PostgreSQL, etc.
Python also supports Data Definition Language (DDL), Data Manipulation Language
(DML) and Data Query Statements. The Python standard for database interfaces is the
Python DB-API. Most Python database interfaces adhere to this standard. You must
download a separate DB API module for each database you need to access.
Python MySQL
Python MySQL Connector is a Python driver that helps to integrate Python and
MySQL. This Python MySQL library allows the conversion between Python and
MySQL data types. MySQL Connector API is implemented using pure Python and does
not require any third-party library.
Python JSON
JSON JavaScript Object Notation is a format for structuring data. It is mainly used for
storing and transferring data between the browser and the server. Python too
supports JSON with a built-in package called json. This package provides all the
necessary tools for working with JSON Objects including parsing, serializing,
deserializing, and many more.
Python MongoDB
MongoDB is one of the most popular NoSQL database. It is a cross-platform, object-
oriented database. Basically NoSQL means MongoDB does not store data in the table
or relational format rather provide a different mechanism for storage and retrieval
of data. This is called BSON which is similar to JSON. That’s why MongoDB offers high
speed, high availability, and high scalability.
Python SQLite
Python SQLite3 module is used to integrate the SQLite database with Python. It is a
standardized Python DBI API 2.0 and provides a straightforward and simple-to-use
interface for interacting with SQLite databases. There is no need to install this module
separately as it comes along with Python after the 2.5x version.
Python SQLite is used to demonstrate how to develop Python database applications
with the SQLite database. You will learn how to perform SQLite database operations
1 Naipunnya School of Management, Cherthala [Jithin Babu]
BCA / BSc Computer Science CS1541/ CP1442: PYTHON PROGRAMMING [4]
from Python. SQLite comes built-in with most of the computers and mobile devices
and browsers. Python’s official sqlite3 module helps us to work with the SQLite
database.
In this chapter we will see the use of SQLite database in python programming language.
It is done by using python’s inbuilt, sqlite3 module. You should first create a connection
object that represents the database and then create some cursor objects to execute SQL
statements.
Connecting to the Database
Connecting to the SQLite Database can be established using the connect() method,
passing the name of the database to be accessed as a parameter. If that database does
not exist, then it’ll be created.
sqliteConnection = [Link]('[Link]')
But what if you want to execute some queries after the connection is being made. For
that, a cursor has to be created using the cursor() method on the connection
instance, which will execute our SQL queries.
cursor = [Link]()
The SQL query to be executed can be written in form of a string, and then executed
by calling the execute() method on the cursor object. Then, the result can be fetched
from the server by using the fetchall() method, which in this case, is the SQLite
Version Number.
query = 'SQL query;'
[Link](query)
result = [Link]()
print('SQLite Version is {}'.format(result))
[Link]([cursorClass])
This routine creates a cursor which will be used throughout your database
programming with Python. This method accepts a single optional parameter
cursorClass. If supplied, this must be a custom cursor class that extends [Link].
[Link](sql [, optional parameters])
This routine executes an SQL statement. The SQL statement may be parameterized (i.
e. placeholders instead of SQL literals). The sqlite3 module supports two kinds of
placeholders: question marks and named placeholders (named style).
For example − [Link]("insert into people values (?, ?)", (who, age))
[Link](sql [, optional parameters])
This routine is a shortcut of the above execute method provided by the cursor object
and it creates an intermediate cursor object by calling the cursor method, then calls
the cursor's execute method with the parameters given.
[Link](sql, seq_of_parameters)
This routine executes an SQL command against all parameter sequences or mappings
found in the sequence sql.
[Link](sql[, parameters])
This routine is a shortcut that creates an intermediate cursor object by calling the
cursor method, then calls the [Link] method with the parameters given.
[Link]()
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.
[Link]()
This method rolls back any changes to the database since the last call to commit().
[Link]()
This method closes the database connection. Note that this does not automatically call
commit(). If you just close your database connection without calling commit() first,
your changes will be lost!
[Link]()
This method fetches the next row of a query result set, returning a single sequence, or
None when no more data is available.
[Link]([size = [Link]])
This routine fetches the next set of rows of a query result, returning a list. An empty
list is returned when no more rows are available. The method tries to fetch as many
rows as indicated by the size parameter.
[Link]()
This routine fetch all (remaining) rows of a query result, returning a list. An empty list
is returned when no rows are available.
Connect To Database
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 = [Link]('[Link]')
Create a Table
Following Python program will be used to create a table in the previously created
database.
import sqlite3
conn = [Link]('[Link]')
print("Opened database successfully")
[Link]('''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")
[Link]()
Insert Operation
Following Python program shows how to create records in the COMPANY table created
in the above example.
import sqlite3
conn = [Link]('[Link]')
print ("Opened database successfully")
[Link]("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (1, 'Paul', 32, 'California', 20000.00 )");
Iterators in Python
Python’s iterators and iterables are two different but related tools that come in
handy when you need to iterate over a data stream or container. Iterators power and
control the iteration process, while iterables typically hold data that you want to
iterate over one value at a time.
Iterate the characters of a string:
mystr = "Naipunnya"
for x in mystr:
print(x)
The for loop actually creates an iterator object and executes the next() method for each
loop.
In the following iterations, the iteration state and iterator variable is managed
internally (we can’t see it) using an iterator object to traverse over the built-in iterable
like list, tuple, dict, etc.
# Iterating over a list
l = ["Naipunnya", "School", "of", "Management"]
for i in l:
print(i)
# Iterating over a tuple (immutable)
t = ("Naipunnya", "School", "of", "Management")
for i in t:
print(i)
# Iterating over dictionary
d = dict()
d['xyz'] = 123
d['abc'] = 345
for i in d :
print("%s %d" %(i, d[i]))
An iterator in Python is an object that is used to iterate over iterable objects like lists,
tuples, dicts, and sets. The Python iterators object is initialized using the iter() method.
It uses the next() method for iteration.
__iter__() : The iter() method is called for the initialization of an iterator. This returns
an iterator object
__next__() : The next method returns the next value for the iterable. When we use a for
loop to traverse any iterable object, internally it uses the iter() method to get an
iterator object, which further uses the next() method to iterate over. This method
raises a StopIteration to signal the end of the iteration.
Example
mystr = "Naipunnya"
myit = iter(mystr)
print(next(myit))
print(next(myit))
print(next(myit))
myclass = MyNumbers()
myiter = iter(myclass)
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
To prevent the iteration from going on forever, we can use
the StopIteration statement.
In the __next__() method, we can add a terminating condition to raise an error if the
iteration is done a specified number of times:
class MyNumbers:
def __iter__(self):
self.a = int(input("Enter starting number : "))
return self
def __next__(self):
if self.a <= 20:
x = self.a
self.a += 1
return x
else:
raise StopIteration
myclass = MyNumbers()
for x in myclass:
print(x)
CGI Programming
The Common Gateway Interface, or CGI, is a set of
standards that define how information is exchanged
between the web server and a custom script. CGI is a
standard for external gateway programs to interface
with information servers such as HTTP servers.
Whenever we click on a hyperlink to browse a particular
web page or URL,
1. Our browser interacts with the HTTP web server
and asks for the same URL (or filename).
2. Web Server then parses the URL and looks for the same filename.
3. If that file is found, then that file is sent back to the browser, otherwise, an error
message is sent indicating that we are demanding the wrong file.
4. Web browser takes the response from a web server and displays it, then
whether it is the received file from the webserver or an error message.
But, conversely, it is possible to set up the HTTP server so that whenever a specific file
is requested, then that file is not sent back, but instead, it is executed as a program, and
whatever that program output is, that is sent back to our browser for display. This
same function is called the Common Gateway Interface (or CGI) and the programs
which are executed are called CGI scripts. In python, these CGI programs are Python
Script.
However, it is possible to set up the HTTP server so that whenever a file in a certain
directory is requested that file is not sent back; instead, it is executed as a program,
and whatever that program outputs is sent back for your browser to display. This
function is called the Common Gateway Interface or CGI and the programs are called
CGI scripts.
Before you proceed with CGI Programming, make sure that your Web Server supports
CGI and it is configured to handle CGI Programs. All the CGI Programs to be executed
by the HTTP server are kept in a pre-configured directory. This directory is called CGI
Directory and by convention it is named as “C:\xampp\htdocs” (as I am using xampp
as web server). By convention, CGI files have extension as. cgi, but you can keep your
files with python extension .py as well.
The following line should also be added for apache server to treat .py file as cgi script.
AddHandler cgi-script .py
Here, we assume that you have Web Server up and running successfully and you are
able to run CGI programs.
Example:
#! C:/Users/admin/AppData/Local/Programs/Python/Python312/[Link]
import cgitb
[Link]()
print("Content-Type: text/html\n")
print ("<html>")
print ("<head>")
print ("<title>My First CGI-Program </title>")
print ("</head>")
print ("<body>")
print ("<h3>This is HTML's Body section </h3>")
print ("</body>")
print ("</html>")
This is a sample program and I am using xampp as webserver. First line in the script
must be the path to Python executable. It appears as a comment in Python program,
but it is called shebang line, and I am saving this file as [Link] in a folder named Py and
which is placed in the CGI Directory named “htdocs” of xampp.
This [Link] script is a simple Python script, which writes its output on STDOUT file,
i.e., screen. There is one important and extra feature available which is first line to be
printed “Content-Type: text/html\n”. This line is sent back to the browser and it
specifies the content type to be displayed on the browser screen.
To execute the above program, enter the following URL in your browser –
[Link]
HTTP Header in CGI Programming
The line Content-type:text/html\r\n\r\n is part of HTTP header which is sent to the
browser to understand the content. All the HTTP header will be in the following form
There are few other important HTTP headers, which we will use frequently in our CGI
Programming.
Header Description
A MIME string defining the format of the file being
Content-type:
returned. Example is Content-type:text/html
The date the information becomes invalid. It is used by the
browser to decide when a page needs to be refreshed. A
Expires: Date
valid date string is in the format 01 Jan 1998 [Link]
GMT.
The URL that is returned instead of the URL requested.
Location: URL
You can use this field to redirect a request to any file.
Last-modified: Date The date of last modification of the resource.
The length, in bytes, of the data being returned. The
Content-length: N browser uses this value to report the estimated download
time for a file.
Set-Cookie: String Set the cookie passed through the string
On the other hand, HTTP headers are part of the HTTP protocol and are used for
communication between the client (usually a web browser) and the server. HTTP
headers provide additional information about the request or response, such as the
content type, content length, and various other metadata. Headers are sent as part of
the HTTP request from the client to the server and in the HTTP response from the
server to the client.
While environment variables can be used to pass information from the web server to
the CGI script, they are not directly related to HTTP headers. The two serve different
purposes: environment variables are about the execution environment of the CGI
script, while HTTP headers are about the communication between the client and the
server.
GET and POST Methods
You must have come across many situations when you need to pass some information
from your browser to web server and ultimately to your CGI Program. Most frequently,
browser uses two methods to pass this information to web server. These methods are
GET Method and POST Method.
Passing Information using GET method
The GET method sends the encoded user information appended to the page request.
The page and the encoded information are separated by the ? character as follows −
[Link]
The GET method is the default method to pass information from the browser to the
web server and it produces a long string that appears in your browser's Location:box.
Never use GET method if you have password or other sensitive information to pass to
the server. The GET method has size limitation: only 1024 characters can be sent in a
request string. You can pass information by simply concatenating key and value pairs
along with any URL or you can use HTML <FORM> tags to pass information using GET
method.
Here is a simple URL, which passes two values to [Link] program using GET method.
/localhost/Py/[Link]?first_name=Ajesh&last_name=Kumar
Given below is the hello_get.py script to handle the input given by web browser. We
are going to use the cgi module, which makes it very easy to access the passed
information −
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
13 Naipunnya School of Management, Cherthala [Jithin Babu]
BCA / BSc Computer Science CS1541/ CP1442: PYTHON PROGRAMMING [4]
form = [Link]()
# Get data from fields
first_name = [Link]('first_name')
last_name = [Link]('last_name')
print ("Content-type:text/html")
print()
print ("<html>")
print ('<head>')
print ("<title>Hello - Second CGI Program</title>")
print ('</head>')
print ('<body>')
print ("<h2>Hello %s %s</h2>" % (first_name, last_name))
print ('</body>')
print ('</html>')
Forms in CGI Programming
This example passes two values using HTML FORM and submit button. We use same
CGI script [Link] to handle this input.
<form action = "/cgi-bin/hello_get.py" method = "get">
First Name: <input type = "text" name = "first_name"> <br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
Here is the actual output of the above form, you enter First and Last Name and then
click submit button to see the result.
First Name:
Last Name:
Passing Information Using POST Method
A generally more reliable method of passing information to a CGI program is the POST
method. This packages the information in exactly the same way as GET methods, but
instead of sending it as a text string after a ? in the URL it sends it as a separate message.
This message comes into the CGI script in the form of the standard input.
There is only a change in script of form tag, we have to specify the method as post as
shown below
<form action = "/cgi-bin/hello_get.py" method = "post">
And the same [Link] script can handles GET as well as POST method.
14 Naipunnya School of Management, Cherthala [Jithin Babu]
BCA / BSc Computer Science CS1541/ CP1442: PYTHON PROGRAMMING [4]
else:
subject = "Not entered"
print ("Content-type:text/html\r\n\r\n")
print ("<html>")
print ("<head>")
print ("<title>Dropdown Box - Sixth CGI Program</title>")
print ("</head>")
print ("<body>")
print ("<h2> Selected Subject is %s</h2>" % subject)
print ("</body>")
print ("</html>")
Using Cookies in CGI
HTTP protocol is a stateless protocol. For a commercial website, it is required to
maintain session information among different pages. For example, one user
registration ends after completing many pages. How to maintain user's session
information across all the web pages?
In many situations, using cookies is the most efficient method of remembering and
tracking preferences, purchases, commissions, and other information required for
better visitor experience or site statistics.
How It Works?
Your server sends some data to the visitor's browser in the form of a cookie. The
browser may accept the cookie. If it does, it is stored as a plain text record on the
visitor's hard drive. Now, when the visitor arrives at another page on your site, the
cookie is available for retrieval. Once retrieved, your server knows/remembers what
was stored.
Cookies are a plain text data record of 5 variable-length fields −
Expires − The date the cookie will expire. If this is blank, the cookie will expire when
the visitor quits the browser.
Domain − The domain name of your site.
Path − The path to the directory or web page that sets the cookie. This may be blank
if you want to retrieve the cookie from any directory or page.
Secure − If this field contains the word "secure", then the cookie may only be
retrieved with a secure server. If this field is blank, no such restriction exists.
Name = Value − Cookies are set and retrieved in the form of key and value pairs.
Setting up Cookies
18 Naipunnya School of Management, Cherthala [Jithin Babu]
BCA / BSc Computer Science CS1541/ CP1442: PYTHON PROGRAMMING [4]
It is very easy to send cookies to browser. These cookies are sent along with HTTP
Header before to Content-type field. Assuming you want to set UserID and Password
as cookies. Setting the cookies is done as follows −
print ("Set-Cookie:UserID = XYZ;\r\n")
print ("Set-Cookie:Password = XYZ123;\r\n")
print ("Set-Cookie:Expires = Tuesday, 31-Dec-2007 [Link] GMT;\r\n")
print ("Set-Cookie:Domain = [Link];\r\n")
print ("Set-Cookie:Path = /perl;\n")
print ("Content-type:text/html\r\n\r\n")
...........Rest of the HTML Content....
From this example, you must have understood how to set cookies. We use Set-
Cookie HTTP header to set cookies.
It is optional to set cookies attributes like Expires, Domain, and Path. It is notable that
cookies are set before sending magic line "Content-type:text/html\r\n\r\n.
Retrieving Cookies
It is very easy to retrieve all the set cookies. Cookies are stored in CGI environment
variable HTTP_COOKIE and they will have following form −
key1 = value1;key2 = value2;key3 = value3....
Here is an example of how to retrieve cookies.
# Import modules for CGI handling
from os import environ
import cgi, cgitb
if environ.has_key('HTTP_COOKIE'):
for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')):
(key, value ) = split(cookie, '=');
if key == "UserID":
user_id = value
if key == "Password":
password = value
print ("User ID = %s" % user_id)
print ("Password = %s" % password)
File Upload Example
To upload a file, the HTML form must have the enctype attribute set to multipart/form-
data. The input tag with the file type creates a "Browse" button.
<html>
<body>
<form enctype = "multipart/form-data" action = "save_file.py" method = "post">
<p>File: <input type = "file" name = "filename" /></p>
<p><input type = "submit" value = "Upload" /></p>
</form>
</body>
</html>
Above example has been disabled intentionally to save people uploading file on our
server, but you can try above code with your server.
Here is the script save_file.py to handle file upload −
import cgi
import os
import cgitb
[Link]()
form = [Link]()
fileitem = form['filename']
if [Link]:
fn=[Link]([Link])
ob=open(fn,'wb')
data=[Link]()
[Link](data)
message = "Uploaded successfully"
else:
message="Cannot Upload the file..."
print ("""\
Content-Type: text/html\n
<html>
<body>
<p>%s</p>
</html>
""" % (message))