Salvaña Ruel James - Module 8

You might also like

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

MODULE 8

Integrative Programming & Technology

PYTHON DATABASE ACCESS

BSIT 3A

Prepared by:

REYNEL M. MARGELINO
Faculty
Lesson Objectives:
At the end of this lesson, you will be able to:
• Setup XAMPP as local web server
• Learn how to use phpMyAdmin as a free software tool in managing
databases, tables, columns, relations, indexes, users, permissions, etc.
• Learn how to connect flask to mysql database and you can insert the
form data.
• Build a CRUD (Create, Read, Update, Delete) web application with the
integration of HTML, CSS, Python (FLASK) and MySQL.

PYTHON MYSQL DATABASE ACCESS


XAMPP
• XAMPP is an open source software developed by Apache friends.
• This local server works on your own desktop or laptop computer.
• The use of XAMPP is to test the clients or your website before uploading it
to the remote web server.
SQL
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases

WHAT CAN SQL DO?

• SQL can execute queries against a database


• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views

SETUP YOUR LOCAL SERVER


1. Download XAMPP and install it.
2. Click start button of Apache and MySQL

3.
4. Click MySQL Admin button.

5. Go to User Accounts and click add user account


Note:

• phpMyAdmin is a free software tool written in PHP, intended to handle


the administration of MySQL over the Web.

• phpMyAdmin supports a wide range of operations on MySQL and


MariaDB. Frequently used operations (managing databases, tables,
columns, relations, indexes, users, permissions, etc) can be performed
via the user interface, while you still have the ability to directly execute
any SQL statement.

6. Setup you credentials and click check all box


7. Click Go to save changes

8. Click log-out
CREATE DATABASE AND TABLE

1. Log-in Again to your phpMyAdmin and create database. Click New and
Input desire database name flask_db and press create button

Note: Databases are useful for storing information categorically.

2. Click the database and create the desire table name and numbers of column
and press Create to save Changes
3.

Note: Auto-increment allows a unique number to be generated automatically


when a new record is inserted into a table.

Often this is the primary key field that we would like to be created automatically
every time a new record is inserted.

4. Open CMD and prompt(need an Internet Connection)

Code:

a. cd documents
b. cd f2
c. py –m venv env
d. env\Scripts\activate
e. pip install flask
f. pip install Flask-MySQLdb.

Notification:
Running setup.py install for Flask-MySQLdb ... done
Successfully installed Flask-MySQLdb-0.2.0 mysqlclient-1.4.6

5. On your flask folder f2 create new text document save it as main. py and
new folder named templates and that will handle all html file. Copy the
code below for main.py

Import of SQL function

SQL credentials from


phpMyAdmin(depends on
whatdid you input)

POST method with route

the name of the html file

Variables and Request form


function for extraction of data
from input tag class name
SQL Command
Return html file

Note:

mysqlcursor.execute() Method

This method executes the given database operation (query or command). The
parameters found in the tuple or dictionary params are bound to the variables
in the operation. Specify variables using %s or %(name)s parameter style (that is,
using format or pyformat style). execute() returns an iterator if multi is True.

mysqlconnection.commit() Method

This method sends a COMMIT statement to the MySQL server, committing the
current transaction. Since by default Connector/Python does not autocommit, it
is important to call this method after every transaction that modifies data for
tables that use transactional storage engines.

6. Create html file inside templates folder and create a link


OUTPUT

7. Copy the code below and save it to main.py


Function name send()

Note: The INSERT INTO statement is used to insert new records in a table.
Copy the code below and save it to insert. html

OUTPUT
8. Inside main.py add the code below for update function

Note: The UPDATE statement is used to modify the existing records in a table.
9. Create new file update.html save it to templates folder.
OUTPUT

10. Inside main.py add the code below for delete function

Note: The DELETE statement is used to delete existing records in a table


11. Create new file delete.html save it to templates folder.

12. Inside main.py add the code below at the bottom part of the file

Note: The SELECT statement is used to select data from a database.


13. Create select. Html save it to templates folder and copy the code below

OUTPUT
SUMMARY

• XAMPP is an open source software and a local server works desktop or


laptop computer.

• phpMyAdmin is a free software tool written in PHP, intended to handle


the administration of MySQL over the Web.

• SQL stands for Structured Query Language lets you access and
manipulate databases.
• In SQL auto-increment allows a unique number to be generated
automatically when a new record is inserted into a table.
• mysqlcursor.execute() is a method executes the given
database operation (query or command).
• mysqlconnection.commit() is a method that sends a commit statement to
the mysql server, committing the current transaction.
• The INSERT INTO statement is used to insert new records in a table.
• The UPDATE statement is used to modify the existing records in a table.
• The DELETE statement is used to delete existing records in a table.
• The SELECT statement is used to select data from a database.
QUESTIONNAIRE
TRUE OR FALSE
Instructions:
✓ Shade the letter A if your answer is True and B if False on the answer on the
separate sheet.

1. XAMPP is an open source software developed by Apache friends.


2. SQL stands for Stratified Query Language.
3. SQL can create new databases
4. SQL can set permissions on tables, procedures, and views.
5. SQL cannot set permissions on tables, procedures, and views.
6. SQL lets you access and manipulate databases.
7. To pip install Flask-MySQLdb you will need an internet connection.
8. It is okay to create a table in MySQL without database.
9. Auto-increment allows a unique number to be generated automatically
when a new record is inserted into a table.

MULTIPLE CHOICE
Instructions:
✓ Write the letter of the correct answer on the separated sheet.
✓ Choose the best answer
10. is a method that sends a COMMIT statement to the
MySQL server, committing the current transaction.

A. MySQLCursor.execute() Method
B. MySQLConnection.commit() Method
C. SQL Command
D. CRUD
E. None of These

11. is the method which executes the given


database operation (query or command)
A. MySQLCursor.execute() Method
B. MySQLConnection.commit() Method
C. SQL Command
D. CRUD
E. None of These

12. is a free software tool written in PHP, intended to


handle the administration of MySQL over the Web.
A. Flask
B. Python
C. phpMyAdmin
D. None of These

13. XAMPP is works on your own desktop or laptop computer.


A. Web Server
B. Application Server
C. Real-Time Communication Server
D. Local Server
14. ___ a free and open-source tool collection for creating responsive
websites and web applications. It is the most popular HTML, CSS, and
JavaScript framework for developing responsive, mobile-first web sites.
A. XAMPP
B. PhpMyAdmin
C. Viewport
D. Python
E. None of These

15. statement is used to insert new records in a table.


A. INSERT INTO
B. UPDATE
C. SELECT
D. DELETE
E. NONE OF THESE
16. statement is used to modify the existing records in a
table.
A. INSERT INTO
B. UPDATE
C. SELECT
D. DELETE
E. NONE OF THESE
17. statement is used to delete existing records in a table.
A. INSERT INTO
B. UPDATE
C. SELECT
D. DELETE
E. NONE OF THESE

You might also like