ADVANCE PROGRAMMING
M. Salim Hamdard
1
Assistant Professor,
Department of Software Engineering,
Kabul University, Afghanistan.
08/15/2025
LEARNING OUTCOME
Should know about DBMS and SQL
To learn creating database connection in python
To learn how to perform various operations on
data
Practical work
2
DATABASE MANAGEMENT SYSTEM
If an application needs to store only a small
amount of data, traditional files work well.
These types of files are not practical, however,
when a large amount of data must be stored and
manipulated.
A database management system (DBMS) is
software that is specifically designed to store,
retrieve, and manipulate large amounts of data
in an organized and efficient manner.
3
CONT..
An application that is developed in Python or
another language can be written to use a DBMS
to manage its data.
Rather than retrieving or manipulating the data
directly, the application can send instructions to
the DBMS.
The DBMS carries out those instructions and
sends the results back to the application.
4
CONT..
5
SQL
SQL, which stands for Structured Query
Language, is a standard language for working
with the DBMS.
SQL was originally developed by IBM in the
1970s. Since then, SQL has been adopted by
most database software vendors as the language
of choice for interacting with its DBMS.
SQL statements are submitted to the DBMS, and
are instructions for the DBMS to carry out
operations on its data.
6
CREATING NEW DATABASE
CREATE DATABASE statement is used to create a
new database.
7
USE EXISTING DATABASE
To use or select existing database, USE
statement is used.
8
CREATING TABLE
CREATE TABLE statement is used to create a
database table.
9
INSERTING VALUES
10
RETRIEVING DATA
11
UPDATING COLUMN VALUE
12
DELETING ROW
13
MYSQL
There are numerous DBMSs in use today, and
Python can interact with many of them.
Some of the more popular ones are MySQL,
Oracle, Microsoft SQL Server, PostgreSQL, and
SQLite.
In this course, we use MySQL because it is easy
to use. To use MySQL in Python, you must install
the latest mysql-connetor for this purpose.
14
CONNECTING TO DATABASE SERVER
In order to connect to MySQL server you also
need to install the latest mysql-connetor for
this purpose.
Use pip install mysql-connector in the
command window to download and install it.
import mysql.connector
con = mysql.connector.connect(host="localhost",
user="root", passwd="")
mycursor = con.cursor()
con.close()
15
CREATING DATABASE
You can easily create a database from python
script as follow:
import mysql.connector
con = mysql.connector.connect(host="localhost",
user="root", passwd="")
mycursor = con.cursor()
mycursor.execute("DROP DATABASE IF EXISTS student")
mycursor.execute("CREATE DATABASE student")
con.close()
16
CREATING TABLE
Create a database using python script as follow:
import mysql.connector
con = mysql.connector.connect(host="localhost",
user="root", passwd="")
mycursor = con.cursor()
mycursor.execute("USE student")
mycursor.execute("DROP TABLE IF EXISTS studentinfo")
mycursor.execute("CREATE TABLE studentinfo (name
VARCHAR(30), age INT(3), gender CHAR(1))")
con.close()
17
INSERTING DATA INTO THE TABLE
Here is a program that show how we can insert
data: mysql.connector
import
con = mysql.connector.connect(host="localhost",
user="root", passwd="")
mycursor = con.cursor()
mycursor.execute("USE student")
sql = """INSERT INTO studentinfo(name, age, gender)
VALUES('Ahmad',25,'M')"""
mycursor.execute(sql)
con.commit()
con.close()
18
INSERTING MULTIPLE ROWS
SIMULTANEOUSLY
Here we are going to use the executemany()
function that accept two parameters as shown
below:
import mysql.connector
con = mysql.connector.connect(host="localhost",
user="root", passwd="")
mycursor = con.cursor()
mycursor.execute("USE student")
sql = """INSERT INTO studentinfo(name, age, gender)
VALUES(%s, %s, %s)"""
rows = [('Hamid', 18,'M'),(‘Mariam', 17, 'F')]
mycursor.executemany(sql, rows)
con.commit()
con.close()
19
READING FORM DATABASE TABLE
fetchone() − It fetches the next row of a query
result set. A result set is an object that is
returned when a cursor object is used to query a
table.
fetchall() − It fetches all the rows in a result set.
If some rows have already been extracted from
the result set, then it retrieves the remaining
rows from the result set.
20
CONT..
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root",
passwd="", database="student")
mycursor = con.cursor()
sql = "SELECT * FROM studentinfo"
mycursor.execute(sql)
result = mycursor.fetchall()
for row in result:
name = row[0]
age = row[1]
gender = row[2]
print("Name=%s, Age=%d, Gender=%c" % (name,age,gender))
con.close()
21
UPDATING RECORDS IN A TABLE
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root",
passwd="", database="student")
mycursor = con.cursor()
sql = "UPDATE studentinfo SET age=22 WHERE name='Hamid'"
mycursor.execute(sql)
sql = "SELECT * FROM studentinfo"
mycursor.execute(sql)
result = mycursor.fetchall()
for row in result:
name = row[0]
age = row[1]
gender = row[2]
print("Name=%s, Age=%d, Gender=%c" % (name,age,gender))
con.close()
22
DELETING RECORDS IN A TABLE
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root",
passwd="", database="student")
mycursor = con.cursor()
sql = "DELETE FROM studentinfo WHERE name='Hamid'"
mycursor.execute(sql)
sql = "SELECT * FROM studentinfo"
mycursor.execute(sql)
result = mycursor.fetchall()
for row in result:
name = row[0]
age = row[1]
gender = row[2]
print("Name=%s, Age=%d, Gender=%c" % (name,age,gender))
con.close()
23
END