Chapter 12: Interface Python With An SQL Database
Chapter 12: Interface Python With An SQL Database
68
COMPUTER SCIENCE
mydb=mysql.connector.connect(host="localhost",user="root",passwd="12345",database="student")
mycursor=mydb.cursor()
mycursor.execute("DESC STUDENT")
for x in mycursor:
print(x)
# SELECT QUERY
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()
#WHERE CLAUSE
import mysql.connector
conn=mysql.connector.connect(host="localhost",user="root",passwd="12345",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)
# DYNAMIC INSERTION
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="12345",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")
# UPDATE COMMAND
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="12345",database="student")
mycursor=mydb.cursor()
mycursor.execute("UPDATE STUDENT SET MARKS=100 WHERE MARKS=40")
mydb.commit()
print(mycursor.rowcount,"RECORD UPDATED")
# DELETE COMMAND
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="12345",database="student")
mycursor=mydb.cursor()
mycursor.execute("DELETE FROM STUDENT WHERE MARKS<50")
mydb.commit()
print(mycursor.rowcount,"RECORD DELETED")
# DROP COMMAND
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="12345",database="student")
mycursor=mydb.cursor()
mycursor.execute("DROP TABLE STUDENT")
# ALTER COMMAND
import mysql.connector
69
COMPUTER SCIENCE
mydb=mysql.connector.connect(host="localhost",user="root",passwd="12345",database="student")
mycursor=mydb.cursor()
mycursor.execute("ALTER TABLE STUDENT ADD GRADE CHAR(3)")
(1 mark question)
Q.1.1 What is database.
Ans. The database is a collection of organized information that can easily be used, managed, update, and they are
classified according to their organizational approach.
Q.1.2 Write command to install connector.
Ans. pip install mysql-connector-python
Q.1.3 Write command to import connector.
Ans. import mysql.connector
(2 mark question)
Q.2 write the steps of connectivity between SQL and Python
Ans. import,connect,cursor,execute
Environment Description
Variables
ROLLBACK It works like "undo", which reverts all the changes that you have made.
Chapter 13: SQL commands: aggregation functions – having, group by, order by
• ORDER BY Clause: You can sort the result of a query in a specific order using ORDER BY clause. The ORDER BY clause allow
sorting of query result by one or more columns. The sorting can be done either in ascending or descending order.
Note: - If order is not specified then by default the sorting will be performed in ascending order.
e.g., Select * from emp order by deptno
Three methods of ordering data are:
1. Ordering data on single column.
2. Ordering data on multiple column.
3. Ordering data on the basis of an expression
• Aggregate Functions: These functions operate on the multiset of values of a column of a relation, and return a value
avg: average value,
min: minimum value ,
max: maximum value ,
sum: sum of values ,
count: number of values
These functions are called aggregate functions because they operate on aggregates of tuples. The result of an aggregate function is
a single value.
e.g., Select deptno, avg (sal), sum (sal) from emp group by deptno
• GROUP BY Clause: The GROUP BY clause groups the rows in the result by columns that have the same values. Grouping is
done on column name. It can also be performed using aggregate functions in which case the aggregate function produces single
value for each group.
e.g., Select deptno, avg (sal), sum (sal) from emp group by deptno
• HAVING Clause: The HAVING clause place conditions on groups in contrast to WHERE clause that place conditions on
individual rows. While WHERE condition cannot include aggregate functions, HAVING conditions can do so.
e.g., Select deptno, avg (sal), sum (sal) from emp group by deptno having deptno=10;
Select job, count (*) from emp group by job having count (*) <3;
Revision of MySQL:-
71