Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Python Database Applications

Uploaded by

Roo Anu
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)
28 views

Python Database Applications

Uploaded by

Roo Anu
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/ 2

Python - Database Applications

MySQL Database
MySQL should have been installed on your computer.

Install MySQL Driver


 Python needs a MySQL driver to access the MySQL database - "MySQL Connector".
 Use PIP to install "MySQL Connector". (Iinstalled in your Python environment)
 Navigate command line to the location of PIP, and type the following:

Download and install "MySQL Connector":


C:\Users\Your Name\AppData\Local\Programs\Python\Python37\Scripts>python -m pip install mysql-
connector

Test MySQL Connector


To test if the installation was successful, or if you already have "MySQL Connector" installed, create a Python
page with the following content:
import mysql.connector

Object and Methods


 MySQL Connector is a standardized database driver for Python and enables Python programs to access
MySQL databases.
 Use the mysql.connector.connect() method of MySQL Connector with required parameters to connect
MySQL.
 Use the connection object returned by a connect() method to create a cursor object to perform Database
Operations.
 The cursor.execute() to execute SQL queries from Python.The parameters found in the tuple are bound to
the variables in the operation. Specify variables using %s.
 MySQLConnection.commit() Method sends a COMMIT statement to the MySQL server, committing the
current transaction.
 MySQLCursor.fetchall() Method fetches all rows of a query result set and returns a list of tuples. If no more
rows are available, it returns an empty list.

Create Connection
Start by creating a connection to the database. Use the username and password from your MySQL database:
import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", passwd=" ")
print(mydb)
Creating a Database
To create a database in MySQL, use the "CREATE DATABASE" statement:
……..
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE school")

The MySQLCursor class instantiates objects that can execute operations such as SQL statements. Cursor objects
interact with the MySQL server using a MySQLConnection object. To create a cursor, use the cursor() method of a
connection object:
Check if Database Exists
You can check if a database exist by listing all databases in your system by using the "SHOW DATABASES" statement:
……..
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")

1 Prepared by Sisira Palihakkara


for x in mycursor:
print(x)
Creating a Table
To create a table in MySQL, use the "CREATE TABLE" statement. Make sure you define the name of the database
when you create the connection.
import mysql.connector
mydb = mysql.connector.connect( host="localhost", user="root", passwd="", database="school" )
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE student (adno CHAR(3), name VARCHAR(15), grade int)")
Check if Table Exists
You can check if a table exist by listing all tables in your database with the "SHOW TABLES" statement:
……..
mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:
print(x)
Insert Into Table
To fill a table in MySQL, use the "INSERT INTO" statement.
………
mycursor = mydb.cursor()
sql = "INSERT INTO student (adno, name, grade) VALUES (%s, %s, %s)"
val = ("101", "Saman", 12)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "Record inserted.")
Select From a Table
To select from a table in MySQL, use the "SELECT" statement:
……….
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM student")
myresult = mycursor.fetchall()
for x in myresult:
print(x)

2 Prepared by Sisira Palihakkara

You might also like