Home Page class 12 @ Python

Python Interface with MySQL

student mysql connectivity programs
Student Data Base With MySQL using mysql.connector

Connecting python with mysql database
to create a database
to create a table within a database
insert the data into table
to insert data into a table by taking input for the details
insert multiple records in the table
to display only one record from a table
to display all records from a table
to display selected columns from the table
searching a record by roll no
searching a record by name
delete by name
update all records
update all records

Student Details connectivity with MySQL using pymysql
Bank Details connectivity with MySQL using pymysql

class 12 Interface python with SQL 4

Program:11

#11
#where condition
#searching a record by name
#select command

import mysql.connector
from mysql.connector import Error
con=None
try:
    con = mysql.connector.connect(host='localhost',user='root',
password='ct',database='student1')
    if(con.is_connected()==True):
        print("connected")
    db=con.cursor()
    sn=input("Enter name of the student ")
    sql=("select * from stud where name='{}'".format(sn))
    db.execute(sql)
    res = db.fetchall()
    for x in res:
        print(x)
except Error as e:
        print(e)
finally:
        if con is not None and con.is_connected():
            con.close()
            print("Connection closed")

Program:12

#12
#delete a record
#delete by name
#to display all records from a table
#select command

import mysql.connector
from mysql.connector import Error
con=None
try:
    con = mysql.connector.connect(host='localhost',user='root',
password='ct',database='student1')
    if(con.is_connected()==True):
        print("connected")
    db=con.cursor()
    sn=input("Enter name ")
    sql="select * from stud where name='{}'".format(sn)
    db.execute(sql)
    res = db.fetchall()
    print("records to be deleted are")
    for x in res:
        print(x)
    sql="delete from stud where name='{}'".format(sn)
    db.execute(sql)
    print(db.rowcount, "record deleted.")
    con.commit();
except Error as e:
        print(e)
finally:
        if con is not None and con.is_connected():
            con.close()
            print("Connection closed")

Program:13

#13
#update all records
#to display all records from a table
#select command

import mysql.connector
from mysql.connector import Error
con=None
try:
    con = mysql.connector.connect(host='localhost',user='root',
password='ct',database='student1')
    if(con.is_connected()==True):
        print("connected")
    db=con.cursor()
    sql="update stud set total=m1+m2+m3+m4+m5"
    db.execute(sql)
    sql="select * from stud"
    db.execute(sql)
    res = db.fetchall()
    for x in res:
        print(x)
    con.commit();
except Error as e:
        print(e)
finally:
        if con is not None and con.is_connected():
            con.close()
            print("Connection closed")