L9 Python and MySQL DB Connection
L9 Python and MySQL DB Connection
L9 Python and MySQL DB Connection
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.
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:
dataBase = mysql.connector.connect(
host ="localhost",
user ="root",
passwd =""
)
# creating database
cursorObject.execute("CREATE DATABASE SOFTWARE")
print("SOFTWARE Data base is created")
# 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")
You can insert one row or multiple rows at once. The connector code is required to connect the
commands to the particular database.
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()
OUTPUT:
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:
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)