Search code examples
pythonpymysql

PyMysql-Python:How to check if an input a user gives exists in my database


I have a table like this:

Movies:
Title    Rank
a        5
b        6
b        6.5
c        7

lets say thats my table.I want to find if an input(title of a movie) a user gives from his keyboard exists in my database and if the movie that he selected exists only once in the table.


Solution

  • If you want to check with sql query

    >>> user_input = input('Enter movie title: ')
    >>> sql_query = 'select title from movie where title = {}'.format(user_input)
    >>> cursor.execute(sql_query)
    >>> result = cursor.fetchone()
    >>> row = result[0]
    

    Or if your query result is in dataframe:

    >>> user_input in df['Title'].tolist()