Notes And Questions NCERT Class 12 Computer Science Chapter 5 Interface python with SQL Database

Notes for Class 12

Please refer to Interface python with SQL Database Class 12 Computer Science Notes and important questions below. The Class 12 Computer Science Chapter wise notes have been prepared based on the latest syllabus issued for the current academic year by CBSE. Students should revise these notes and go through important Class 12 Computer Science examination questions given below to obtain better marks in exams

Interface python with SQL Database Class 12 Computer Science Notes and Questions

The below Class 12 Interface python with SQL Database notes have been designed by expert Computer Science teachers. These will help you a lot to understand all the important topics given in your NCERT Class 12 Computer Science textbook. Refer to Chapter 5 Interface python with SQL Database Notes below which have been designed as per the latest syllabus issued by CBSE and will be very useful for upcoming examinations to help clear your concepts and get better marks in examinations.

  • 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()-Fetch all (remaining) rows of a query result, returning them as a sequence of sequences (e.g. a list of tuples) fetch many (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
mydb=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”12345″)
mycursor=mydb.cursor()
mycursor.execute(“CREATE DATABASE SCHOOL”)
# SHOW DATABASE
import mysql.connector
mydb=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”12345″)
mycursor=mydb.cursor()
mycursor.execute(“SHOW DATABASE”)
for x in mycursor:
print (x)
# CREATE TABLE
import mysql.connector
mydb=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”12345″,database=”student”)
mycursor=mydb.cursor()
mycursor.execute(“CREATE TABLE FEES (ROLLNO INTEGER(3),NAME VARCHAR(20),AMOUNT INTEGER(10));”)
# SHOW TABLES
import mysql.connector
mydb=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”12345″,database=”student”)
mycursor=mydb.cursor()
mycursor.execute(“SHOW TABLES”)
for x in mycursor:
print(x)
#DESCRIBE TABLE
import mysql.connector
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
mydb=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”12345″,database=”student”)
mycursor=mydb.cursor()
mycursor.execute(“ALTER TABLE STUDENT ADD GRADE CHAR(3)”)