@@ -0,0 +1,1544 @@
+#!/usr/bin/python%0A%0A# we've been outputting stuff to text so now I get to wedge it into a database%0A# funtimes%0A%0A# set up the database with %60sqlite3 netflix_genres.sqlite %3C create_tables.sql%60%0A%0Aimport codecs%0Aimport sqlite3%0Aimport sys%0A%0Aconn = sqlite3.connect('netflix_genres.sqlite')%0Ac = conn.cursor()%0A%0A# for row in c.execute('SELECT id, info FROM movies'):%0A#     db_id = row%5B0%5D%0A#     info = row%5B1%5D.split(',')%0A#     movie_id = info%5B0%5D%0A#     updates.append((movie_id, db_id))%0A# %0A# # print updates%0A# %0A# for update in updates:%0A#     print update%0A#     c.execute('UPDATE movies SET movie_id=? WHERE id=?', update)%0A%0Ac.execute('SELECT DISTINCT (movie_id) FROM movies ORDER BY movie_id')%0Amovie_ids = c.fetchall()%0A%0Ainserts = %5B%5D%0A%0Afor row in movie_ids:%0A    movie_id = row%5B0%5D%0A    name = cover_url = movie_url = None%0A    genre_ids = %5B%5D%0A    for entry in c.execute('SELECT id, name, cover_url, movie_url, genres FROM movies WHERE movie_id = ?', (movie_id,)):%0A        if not name:%0A            name = entry%5B1%5D%0A        if name != entry%5B1%5D:%0A            print %22Row %25s has variant name (%25s not %25s)%22 %25 (entry%5B0%5D, entry%5B1%5D, name)%0A        cover_url = cover_url or entry%5B2%5D%0A        movie_url = movie_url or entry%5B3%5D%0A        genre_ids.append(%22%2505i%22 %25 int(entry%5B4%5D))%0A%0A        # print entry%0A    genres = ','.join(genre_ids)%0A    print (movie_id, name)%0A    inserts.append((name, cover_url, movie_url, genres))%0A%0A#print inserts%0A%0Aprint %22... Inserting%22%0A%0Ac.executemany('INSERT INTO movies_n (name, cover_url, movie_url, genres) VALUES(?,?,?,?)', inserts)%0A%0Aconn.commit()%0Aconn.close()%0A

Document work in fixing movies table