Sunday, January 31, 2021

python day 28 mysql with python crud operations

 # -*- coding: utf-8 -*-

"""

Created on Sun Jan 31 13:29:32 2021


@author:anil




#try fail condition

try:

   print("i am in try")

   

#error handling

except:

    print("i am in expect block")

   

    


#try fail condition

try:

   print(1/0)

   #failing condition

   

#error handling

except:

    print("i am in except block")



   

#try fail condition

try:

   #print("db conneted")

   print(1/0)

   #db connections

   #failing condition

   

#error handling

except ZeroDivisionError:

    print("this is name ZeroDivisionError: block")


try:

   #print("db conneted")

   print(1/1)

   #db connections

   #failing condition

   

#error handling

except ZeroDivisionError:

    print("this is name ZeroDivisionError: block")

else:

    print("nothing is wrong")

 

  

try:

   #print("db conneted")

    while(1):

        print(1/1)

   #db connections

   #failing condition

   

#error handling

except ZeroDivisionError:

    print("this is name ZeroDivisionError: block")

finally:

    print("finally class")


try:

   #print("db conneted")

   for i in range(10):

       print(1/1)

    

   print(1/0)

   #db connections

   #failing condition

   

#error handling

except ZeroDivisionError:

    print("this is name ZeroDivisionError: block")

finally:

    print("finally class")


    

    

try:

   f = open("mytesfd.txt",'r+b')

   f.write("this is testing")

except:

    raise Exception("sorry file is missing")

finally:

    f.close()

   


try:

   print("this is connected")


finally:

    print("file closed")


    

    

import pymysql


conn=pymysql.connect(host="localhost",user="root",password="",db="praveen_test")

mycursor=conn.cursor()

ycursor.execute(CREATE TABLE fifth_class_a (id int primary key, name varchar(20),last_name varchar(20)))

mycursor.execute(CREATE TABLE fifth_class_b (id int primary key, name varchar(20)))

mycursor.execute(CREATE TABLE fifth_class_b (id int primary key, name varchar(20)))

conn.commit()

conn.close()


import pymysql


conn=pymysql.connect(host="localhost",user="root",password="",db="praveen_test")

mycursor=conn.cursor()


mycursor.execute(CREATE TABLE fifth_class_d (id int primary key, name varchar(20),lastname varchar(20),student_id varchar(20)))

conn.commit()

conn.close()



import pymysql


conn=pymysql.connect(host="localhost",user="root",password="",db="praveen_test")

mycursor=conn.cursor()

#mycursor.execute("INSERT INTO names(id,name) VALUES(1,'praveen');")

#a=mycursor.execute("INSERT INTO names(id,name) VALUES(4,'lavanya');")


#print("->data inserted")


 

mycursor.execute("UPDATE `names` SET `name` = 'lavanya2' WHERE `names`.`id` = 4;")


mycursor.execute("SELECT * FROM names")

row = mycursor.fetchone()

while row is not None:

    print(row)

    row = mycursor.fetchone()


conn.commit()

conn.close()


#CRUD

#C-create

#R-read

#U-update

#D-delete



#update the tables

import pymysql


conn=pymysql.connect(host="localhost",user="root",password="",db="praveen_test")

mycursor=conn.cursor() 

mycursor.execute("UPDATE `names` SET `name` = 'laxmi' WHERE `names`.`id` = 4;")


conn.commit()

conn.close()




#delete operation

import pymysql


conn=pymysql.connect(host="localhost",user="root",password="",db="praveen_test")

mycursor=conn.cursor() 

mycursor.execute("DELETE FROM `names`  WHERE `id` = 4;")


conn.commit()

conn.close()




import pymysql


conn=pymysql.connect(host="localhost",user="root",password="",db="praveen_test")

mycursor=conn.cursor() 

#mycursor.execute("DELETE FROM `names`  WHERE `id` = 4;")

sql_select_Query = "select * from names"

mycursor.execute(sql_select_Query)

records = mycursor.fetchall()


for row in records:

        print("Id = ", row[0], )

        print("Name = ", row[1])

        

        

        

conn.commit()

conn.close()


"""


import pymysql


conn=pymysql.connect(host="localhost",user="root",password="",db="praveen_test")

mycursor=conn.cursor() 

#mycursor.execute("DELETE FROM `names`  WHERE `id` = 4;")

sql_select_Query = "select * from names"

mycursor.execute(sql_select_Query)

records = mycursor.fetchall()


for row in records:

        print("Name = ", row[1])


conn.commit()

conn.close()





    

    


No comments:

Post a Comment

python class topic video