をなぜ重複を防ぐために、データベースの構造に依存していますか?
これは、データをロードするときに簡単に実行されます。
次はsqlite3
を使用して、おそらく改善が必要されています
データファイルそのcatalog.txt:
album 1,artist 1,rock,track 1
album 1,artist 1,rock,track 2
album 1,artist 1,rock,track 3
album 1,artist 1,rock,track 4
album 1,artist 1,rock,track 5
album 1,artist 1,rock,track 6
album 1,artist 1,rock,track 7
album 1,artist 1,rock,track 8
album 1,artist 1,rock,track 9
album 1,artist 1,rock,track 10
album 2,artist 1,rock,track 1
album 2,artist 1,rock,track 2
album 2,artist 1,rock,track 3
album 2,artist 1,rock,track 4
album 2,artist 1,rock,track 5
album 3,artist 2,regge,track 1
album 3,artist 2,regge,track 2
album 3,artist 2,regge,track 3
album 2,artist 3,rock,track 1
album 2,artist 3,rock,track 2
album 2,artist 3,rock,track 3
album 2,artist 3,rock,track 4
album 2,artist 3,rock,track 5
コード:
をアーティストがそれが依存している
異なっている場合、これは重複したアルバム名を許可しません新しいレコードを挿入するときのキーのautoincrement
import sqlite3
db_name = "catalog.db"
db = sqlite3.connect(db_name, isolation_level=None)
db.row_factory = sqlite3.Row
cursor = db.cursor()
result = cursor.execute("create table if not exists artist (artist_id integer primary key autoincrement,artist_name char(150))")
result = cursor.execute("create table if not exists genre (genre_id integer primary key autoincrement,genre_name char(150))")
result = cursor.execute("create table if not exists album (album_id integer primary key autoincrement,album_name char(150),artist_id int,genre_id int)")
result = cursor.execute("create table if not exists track (track_id integer primary key autoincrement,track_name char(150),album_id int)")
with open('catalog.txt','r') as f:
data = f.readlines()
for i in data:
track = i.split(',')
alb = track[0].strip()
art = track[1].strip()
gen = track[2].strip()
tra = track[3].strip()
#Check for existing artist
cursor.execute("select artist_id from artist where artist_name = ?",[art])
data = cursor.fetchone()
if data:
art_id = data['artist_id']
else: #Insert new record then record the new id
db.execute("insert into artist(artist_name) values (?)",(art,))
cursor.execute("select artist_id from artist where artist_name = ?",[art])
data = cursor.fetchone()
art_id = data['artist_id']
print "Adding artist", art
#Check for existing genre
cursor.execute("select genre_id from genre where genre_name = ?",[gen])
data = cursor.fetchone()
if data:
gen_id = data['genre_id']
else: #Insert new record then record the new id
db.execute("insert into genre(genre_name) values (?)",(gen,))
cursor.execute("select genre_id from genre where genre_name = ?",[gen])
data = cursor.fetchone()
gen_id = data['genre_id']
print " Adding genre", gen
#Check for existing album by given artist
cursor.execute("select album_id from album where album_name = ? and artist_id = ?",[alb,art_id])
data = cursor.fetchone()
if data:
alb_id = data['album_id']
else: #Insert new record then record the new id
db.execute("insert into album(album_name,artist_id,genre_id) values (?,?,?)",(alb,art_id,gen_id,))
cursor.execute("select album_id from album where album_name = ? and artist_id = ?",[alb,art_id])
data = cursor.fetchone()
alb_id = int(data[0])
print " Adding album", alb
#Check for track in given album
cursor.execute("select track_id from track where album_id = ? and track_name = ?",[alb_id,tra])
data = cursor.fetchone()
if data:
pass # duplicate"
else:
db.execute("insert into track(track_name,album_id) values (?,?)",(tra,alb_id,))
print " Adding", tra, art, alb
可能性はありません2人のアーティストの名前は同じですか? – nilesh