Program:8
#8 #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="select * from stud;" 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:9
#9 #to display all records from a table #to display selected columns from the 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="select rollno,name from stud;" 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:10
#10 #where condition #searching a record by roll no #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() r=int(input("Enter rollno ")) sql=("select * from stud where rollno={}".format(r)) 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")