Advertisement
Guest User

Untitled

a guest
May 26th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. def updateRank(rank1, rank2, movieTitle):
  2.  
  3.     # Create a new connection
  4.     con = connection()
  5.    
  6.     # Create a cursor on the connection
  7.     cursor = con.cursor()
  8.  
  9.     try:
  10.         float(rank1)
  11.     except ValueError:
  12.         return [("status",),("error",),]
  13.     try:
  14.         float(rank2)
  15.     except ValueError:
  16.         return [("status",),("error",),]
  17.  
  18.     # See if rank1, rank2 are out of range [0:10]
  19.     if float(rank1) < 0 or float(rank1) > 10:
  20.         return [("status",),("error",),]
  21.     if float(rank2) < 0 or float(rank2) > 10:
  22.         return [("status",),("error",),]
  23.  
  24.  
  25.     # See if movie exists in the database
  26.     sql_exists = """ select *
  27.                    from movie
  28.                    where exists
  29.                        (select title from movie where title = %s) """
  30.     cursor.execute(sql_exists, str(movieTitle))
  31.     result = cursor.fetchall()
  32.     if result == ():
  33.         return [("status",),("error",),]
  34.  
  35.    
  36.     # See if multiple movies with the same title exist in the database
  37.     sql_multiple = """ select count(distinct m.title)
  38.                       from movie m
  39.                       where m.title = %s """
  40.     cursor.execute(sql_multiple, str(movieTitle))
  41.     result = cursor.fetchall()
  42.     if result > ((1,),):
  43.         return [("status",),("error",),]
  44.  
  45.  
  46.     # Obtain movie's rank
  47.     sql_test_null = "select rank from movie where title = %s"
  48.     cursor.execute(sql_test_null, str(movieTitle))
  49.     result = cursor.fetchall()
  50.     # See if movie's rank is NULL
  51.     if result == ((None,),):
  52.          try:
  53.              sql_query = """ update movie
  54.                             set rank = (%s + %s) / 2
  55.                             where title = %s """
  56.              # Execute the sql command
  57.              cursor.execute(sql_query, (str(rank1), str(rank2), str(movieTitle)))
  58.              # Commit changes in the database
  59.              con.commit()
  60.          except:
  61.              # Rollback in case there is an error
  62.              con.rollback()
  63.     # If movie's rank is NOT NULL
  64.     else:
  65.         try:
  66.             sql_query = """ update movie
  67.                            set rank = (%s + %s + rank) / 3
  68.                            where title = %s """
  69.             # Execute the sql command
  70.             cursor.execute(sql_query, (str(rank1), str(rank2), str(movieTitle)))
  71.             # Commit changes in the database
  72.             con.commit()
  73.         except:
  74.             # Rollback in case there is an error
  75.             con.rollback()
  76.  
  77.     print (rank1, rank2, movieTitle)
  78.    
  79.     return [("status",),("ok",),]
  80.    
  81.     con.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement