Search code examples
pythonsqlitecasting

SQL Type Casting


I am trying to make a mock database to store film timings and allow the user to make bookings from the list. I have run into a problem where I am trying to take the ticket price from a table and multiply it by the number of tickets to get a final price. When I try to run the code it says that you cannot multiply a list by and int. Has anyone an idea on how to cast the value in a table in SQL into an int? Here is the relevant code:

    showTime = input("Enter showtime: ")
    filmRequest = input("Enter the film you want to watch: ").title()

    priceSelection_ = "SELECT price FROM films WHERE film = ? AND timings = ?"
    parSel_ = (filmRequest, showTime)
    
    cursor.execute(priceSelection_, parSel_)
    
    x = cursor.fetchall()

    priceCalc_ = numTick_ * x
    print(priceCalc_)

Solution

  • You do not cast it. You must select the appropriate value from the result set. In your case, it should be

    x = cursor.fetchall()[0][0]
    

    But you may need to further cast/process the value depending on how it is stored.