Warum verlassen sich auf die Struktur der Datenbank Duplikate zu verhindern?
Dies ist einfach, wenn Sie die Daten laden.
Das Folgende ist sqlite3
verwenden und wahrscheinlich muss verbessert werden:
Datendatei 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
Code:
Dies tut doppelte Albumnamen zulassen, wenn der Künstler unterscheidet
es auf dem beruht autoincrement
für die Schlüssel beim Einfügen neuer Datensätze
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
kann es da keine Möglichkeiten geben bei zwei Künstlern den gleichen Namen haben? – nilesh