I have a movie that can have any number of genres. I would like to have that list inside a database, but it seems databases cant hold a list in a cell.
Is there any other way to achieve this, so I can have any number of genres connected to a specific movie?
class Film:
def __init__(self, title, genres):
self.title = title
self.genres = genres
def accept_genre(self, genre):
self.genres.append(genre)
Movie1 = Film("Taxi Driver", ["Drama"])
print(Movie1.genres) #prints ["Drama"]
Movie1.accept_genre("Crime")
print(Movie1.genres) #prints ["Drama", "Crime"]
...
#im thinking something like this but it doesnt work with lists:
def addToDB(title):
with conn:
c.execute("""UPDATE film SET genres = :genre WHERE title = :title""",
{'title': title, 'genres': list,}) addToDB("Taxi Driver")
CodePudding user response:
Have you considered storing as a semicolon delimited string?
genres_list = ['Comedy','Horror']
# Pass this dictionary into cur.execute:
{'title': title, 'genres': ';'.join(genres_list),}
To convert a semicolon delimited string to a list, use the .split method:
'Comedy;Horror'.split(';')
# the result is ['Comedy','Horror']