Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Server-side database management (PYTHON)

Open CMD: Run as ADMIN:

i. pip3 install mysql-connector (PIP Preferred Installer Program)


ii. Open XAMMP Application and start Apache and MySQL.
iii. Create Database of your choice, table and populate.

PYTHON MYSQL CONNECTOR MODULE METHODS

1. Connect (): This function is used for establishing a connection with the MySQL server. The following
are the arguments that are used to initiate a connection:
a. user: User name associated with the MySQL server used to authenticate the connection
b. password: Password associated with the user name for authentication
c. database: Data base in the MySQL for creating the Table
2. Cursor (): Cursor is the workspace created in the system memory when the SQL command is executed.
This memory is temporary and the cursor connection is bounded for the entire session/lifetime and the
commands are executed

3. Execute (): The execute function takes a SQL query as an argument and executes. A query is an SQL
command which is used to create, insert, retrieve, update, delete etc.

CREATE DATABASE NAMED SOFTWARE

Python Database API (Application Program Interface) is the Database interface for the standard Python.
This standard is adhered to by most Python Database interfaces. There are various Database servers
supported by Python Database such as MySQL, GadFly, mSQL, PostgreSQL, Microsoft SQL Server 2000,
Informix, Interbase, Oracle, Sybase etc. To connect with MySQL database server from Python, we need to
import the MySQL. Connector interface.
Syntax:

CREATE DATABASE DATABASE_NAME


Example:
# importing required libraries
import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="root",
passwd =""
)

# preparing a cursor object


cursorObject = dataBase.cursor()

# creating database
cursorObject.execute("CREATE DATABASE SOFTWARE")
print("SOFTWARE Data base is created")

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 1


OUTPUT:

CREATE A TABLE CALLED EMPLOYEE IN A DATABASE CALLED SOFTWARE:

Create a table in the database, then connect to a database.

# importing required library


import mysql.connector

# connecting to the database


dataBase = mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "",
database = "SOFTWARE" )

# preparing a cursor object


cursorObject = dataBase.cursor()

# creating table
studentRecord = """CREATE TABLE EMPLOYEE (
ID INT NOT NULL,
NAME VARCHAR(60) NOT NULL,
AGE INT NOT NULL,
ADDRESS VARCHAR(60) NOT NULL,
SALARY DOUBLE NOT NULL,
PRIMARY KEY(ID)
)"""

# table created
cursorObject.execute(studentRecord)
# disconnecting from server
dataBase.close()
print("EMPLOYEE Table is Created in the Database")

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 2


OUTPUT:

INSERT DATA IN THE TABLE EMPLOYEE (DATABASE SOFTWARE):

You can insert one row or multiple rows at once. The connector code is required to connect the
commands to the particular database.

# Enter the server name in host


# followed by your user and
# password along with the database
# name provided by you.

import mysql.connector

mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "",
database = "SOFTWARE"
)

mycursor = mydb.cursor()
sql = "INSERT INTO EMPLOYEE (ID,NAME,AGE,ADDRESS,SALARY) VALUES (%s, %s, %s, %s,
%s)"
val = [("100", "Joy","23" , "Nakuru", "98000"),
("101", "Wanyonyi", "34", "Busia", "70000"),
("102", "Oletutu", "52","Kajiado", "34600"),
("103", "Akinyi", "40", "Nakuru","74900"),
("104", "Salat", "29", "Garissa", "84200"),
("105", "Wanjiru", "25", "Njoro", "97400")]
mycursor.executemany(sql, val)
mydb.commit()

print(mycursor.rowcount, "details inserted")

# disconnecting from server


mydb.close()

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 3


OUTPUT:

DISPLAY ALL DATABASES IN LOCAL HOST USING PYTHON:


import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="")
mycursor=mydb.cursor()
mycursor.execute("show databases")
for i in mycursor:
print(i)

FETCH DATA FROM DATABASE IN PYTHON:


(Using database SOFTWARE and table employee)
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database="SOF
TWARE")
mycursor=mydb.cursor()
mycursor.execute("Select * from employee")
for i in mycursor:
print(i)

OUTPUT:

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 4


FETCH ALL DATA FROM DB PYTHON (Using a variable GEORGE):

(Using database SOFTWARE and table employee)

import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database="SOF
TWARE")
mycursor=mydb.cursor()
mycursor.execute("Select * from employee")
GEORGE=mycursor.fetchall()
for i in GEORGE:
print(i)

OUTPUT:

FETCH ONE DATA FROM DB PYTHON (Using a variable GEORGE):

(Using database BSCIT and table employee)

import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database="SOF
TWARE")
mycursor=mydb.cursor()
mycursor.execute("Select * from employee")
GEORGE=mycursor.fetchone()
for i in GEORGE:
print(i)

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 5


OUTPUT:

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 6

You might also like