Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def updateRank(rank1, rank2, movieTitle):
- # Create a new connection
- con = connection()
- # Create a cursor on the connection
- cursor = con.cursor()
- try:
- float(rank1)
- except ValueError:
- return [("status",),("error",),]
- try:
- float(rank2)
- except ValueError:
- return [("status",),("error",),]
- # See if rank1, rank2 are out of range [0:10]
- if float(rank1) < 0 or float(rank1) > 10:
- return [("status",),("error",),]
- if float(rank2) < 0 or float(rank2) > 10:
- return [("status",),("error",),]
- # See if movie exists in the database
- sql_exists = """ select *
- from movie
- where exists
- (select title from movie where title = %s) """
- cursor.execute(sql_exists, str(movieTitle))
- result = cursor.fetchall()
- if result == ():
- return [("status",),("error",),]
- # See if multiple movies with the same title exist in the database
- sql_multiple = """ select count(distinct m.title)
- from movie m
- where m.title = %s """
- cursor.execute(sql_multiple, str(movieTitle))
- result = cursor.fetchall()
- if result > ((1,),):
- return [("status",),("error",),]
- # Obtain movie's rank
- sql_test_null = "select rank from movie where title = %s"
- cursor.execute(sql_test_null, str(movieTitle))
- result = cursor.fetchall()
- # See if movie's rank is NULL
- if result == ((None,),):
- try:
- sql_query = """ update movie
- set rank = (%s + %s) / 2
- where title = %s """
- # Execute the sql command
- cursor.execute(sql_query, (str(rank1), str(rank2), str(movieTitle)))
- # Commit changes in the database
- con.commit()
- except:
- # Rollback in case there is an error
- con.rollback()
- # If movie's rank is NOT NULL
- else:
- try:
- sql_query = """ update movie
- set rank = (%s + %s + rank) / 3
- where title = %s """
- # Execute the sql command
- cursor.execute(sql_query, (str(rank1), str(rank2), str(movieTitle)))
- # Commit changes in the database
- con.commit()
- except:
- # Rollback in case there is an error
- con.rollback()
- print (rank1, rank2, movieTitle)
- return [("status",),("ok",),]
- con.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement