Interface python with an SQL database
• Database connectivity: - Database connectivity refers to connection
and communication between an application and a database system.
• Mysql.connector:- Library or package to connect from python to
MySQL.
• Command to install connectivity package: - pip install mysql-
connector-python
• Command to import connector: - import mysql.connector
• Steps for python MySQL connectivity:-
1. Install Python
2. Install MySQL
3. Open Command prompt
4. Switch on internet connection
5. Type pip install mysql-connector-python and execute
6. Open python IDLE
7. import mysql.connector
• Multiple ways to retrieve data:-
fetchall():- Fetchall (remaining) rows of a query result, returning them as
a sequence of sequences (e.g. a list of tuples).
fetchmany (size):-Fetch the next set of rows of a query result, returning a
sequence of sequences. It will return number of rows that matches to the
size argument.
fetchone():-Fetch the next row of a query result set, returning a single
sequence or None when no more data is available.
• Functions to execute SQL queries:-
# CREATE DATABASE
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456")
mycor = mydb.cursor()
mycon.execute("CREATE DATABSE Chinmaya12A")
# SHOW DATABASE
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456")
mycor = mydb.cursor()
mycon.execute("SHOW DATABSES")
for i in mycon:
print (i)
# CREATE TABLE
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456", database =
"Chinmaya12A")
mycor = mydb.cursor()
mycon.execute("CREATE TABLE student (Name char (25), roll int(10), class int(10))")
mydb.commit()
mydb.close()
# SHOW TABLE
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456", database =
"Chinmaya12A")
mycor = mydb.cursor()
mycon.execute("SHOW TABLES")
for i in mycon:
print (i)
# DESCRIBE TABLE
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456", database =
"Chinmaya12A")
mycor = mydb.cursor()
mycon.execute("DESC student")
data = mycon.fetchall()
for i in data:
print (i)
# SELECT QUERY
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456", database =
"Chinmaya12A")
mycor = mydb.cursor()
mycon.execute("SELECT * FROM student")
data = mycon.fetchall()
for i in data:
print (i)
# WHERE CLAUSE
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456", database =
"Chinmaya12A")
mycor = mydb.cursor()
mycon.execute("SELECT * FROM student")
data = mycon.fetchall()
for i in data:
print (i)
# DYNAMIC INSERTION
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456", database =
"Chinmaya12A")
mycor = mydb.cursor()
name = input("Enter Name :-")
roll = input("Enter Roll :-")
class = input("Enter Class :-")
mycon.execute("INSERT INTO student VALUES ('{}', '{}', '{}',)".format (name, roll,
class))
mydb.commit()
mydb.close()
# UPDATE COMMAND
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456", database =
"Chinmaya12A")
mycor = mydb.cursor()
mycon.execute("UPDATE student SET roll = 5 WHERE Name = 'Ram'")
mydb.commit()
mydb.close()
# DELETE COMMAND
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456", database =
"Chinmaya12A")
mycor = mydb.cursor()
mycon.execute("DELET FROM student WHERE roll = 26")
mydb.commit()
mydb.close()
# DROP COMMAND
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456", database =
"Chinmaya12A")
mycor = mydb.cursor()
mycon.execute("DROP TABLE student")
mydb.commit()
mydb.close()
# ALTER COMMAND
import mysql.connector as my
mydb = my.connect(host = "localhost", user = "root", password = "123456", database =
"Chinmaya12A")
mycor = mydb.cursor()
mycon.execute("ALTER TABLE student ADD Marks int (10)")
mydb.commit()
mydb.close()