Interface Python With SQL Database
Interface Python With SQL Database
Interface Python With SQL Database
Contents:
Connecting SQL with Python
Creating database connectivity applications
Performing insert, delete, update,delete queries
Display data by using fetchone(), fetchall(), fetchmany (), rowcount()
Database connectivity
Database connectivity refers to connection and communication between an application
and a database system.
The term “front-end” refers to the user interface, while “back-end” means the server,
application and database that work behind the scenes to deliver information to the user.
The connect statement creates a connection to the mysql server and returns a
MySQL connection object.
Syntax:
con=mysql.connector.connect(host=”localhost”,user=”root”, passwd=” “)
Syntax:
<cursor object>=<connectionobject>.cursor()
Eg: cursor=con.cursor()
5. Execute SQL query:
The above code will execute the sql query and store the retrieved records
(resultset) in the cursor object(cursor).
Result set refers to a logical set of records that are fetched from the database
by executing an sql query and made available in the program.
The records retrieved from the database using SQL select query has to be
extracted as record from the result set. We can extract data from the result
set using the following fetch () function.
fetchall()
fetchone()
fetchmany()
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system
")
mycursor=mydb.cursor()
mycursor.execute("CREATE DATABASE
SCHOOL")
# SHOW DATABASE
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system")
mycursor=mydb.cursor()
mycursor.execute("SHOW
DATABASES") for x in mycursor:
print (x)
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passw
d="s ystem",database="student") mycursor=mydb.cursor()
mycursor.execute("CREATE TABLE FEES (ROLLNO INT,NAME
VARCHAR(20),AMOUNT INT);")
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system
",database="student")
mycursor=mydb.cursor()
mycursor.execute("SHOW
TABLES") for x in mycursor:
print(x)
#TO DESCRIBE TABLE STRUCTURE USING PYTHON INTERFACE
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system"
,database="student")
mycursor=mydb.cursor()
mycursor.execute("DESC
STUDENT") for x in mycursor:
print(x)
import mysql.connector
conn=mysql.connector.connect(host="localhost",user="root",passwd="12345
",database="student")
c=conn.cursor()
c.execute("select * from
student") r=c.fetchone() while
r is not None:
print(r)
r=c.fetchone()
import mysql.connector
conn=mysql.connector.connect(host="localhost",user="root",passwd="123
45", database="student") if conn.is_connected()==False:
print("Error connecting to MYSQL DATABASE")
c=conn.cursor()
c.execute("select * from student where
marks>90") r=c.fetchall() count=c.rowcount
print("total no of rows:",count) for row in r:
print(row)
#TO INSERT A RECORD (ROLLNO,NAME,AND MARKS) IN MYSQL TABLE student
USING #PYTHON INTERFACE
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system
",database="student")
mycursor=mydb.cursor() r=int(input("enter
the rollno")) n=input("enter name")
m=int(input("enter marks"))
mycursor.execute("INSERT INTO
student(rollno,name,marks) VALUES({},'{}',
{})".format(r,n,m)) mydb.commit()
print(mycursor.rowcount,"RECORD INSERTED")
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="s
yste m",database="student") mycursor=mydb.cursor()
mycursor.execute("UPDATE STUDENT SET MARKS=100 WHERE
MARKS=40") mydb.commit() print(mycursor.rowcount,"RECORD
UPDATED")
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system
",database="student") mycursor=mydb.cursor()
mycursor.execute("DELETE FROM STUDENT WHERE
MARKS<50") mydb.commit()
print(mycursor.rowcount,"RECORD DELETED")
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system
",database="student")
mycursor=mydb.cursor()
mycursor.execute("DROP TABLE STUDENT")
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system
",database="student") mycursor=mydb.cursor()
mycursor.execute("ALTER TABLE STUDENT ADD AGE
INT”) mydb.commit()
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system
",database="student")
mycursor=mydb.cursor()
mycursor.execute("ALTER TABLE DROP AGE ”)
mydb.commit()
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="syste
m",database="student")
mycursor=mydb.cursor()
mycursor.execute("ALTER TABLE STUDENT MODIFY GRADE CHAR(3)")