Page 121 - PYTHON-12
P. 121

except:


                      db1.rollback()

                   db1.close()


















                           Fetching all the records from EMP table having salary more than 70000.

                   import MySQLdb


                   db1 = MySQLdb.connect("localhost","root","","TESTDB" )

                   cursor = db1.cursor()

                   sql = "SELECT * FROM EMP WHERE SALARY > 70000;"


                   try:

                        cursor.execute(sql)

                        #using fetchall() function to fetch all records from the table EMP and store in
                   resultset


                        resultset = cursor.fetchall()

                   for row in resultset:




                         print (row)

                   except:


                      print ("Error: unable to fetch data")

                   db1.close()
   116   117   118   119   120   121   122   123   124   125   126