2017-05-28 60 views
0

我有一个关系数据库,我试图学习如何插入行。下面是我得到了什么:如何插入关系表?

artist {id, name} 
genre {id, name} 
album {id, name, artist_id, genre_id} 
track {id, album_id, track_name} 

我最大的问题是,我想artist是唯一的,所以不可能有具有相同名称的艺术家,但是每个查询我尝试插入重复。我尝试将桌子改为独一无二的,但我发现不可能再由一位艺术家添加多首单曲。此时我卡住了;我已经查看了ON DUPLICATE语句而没有成功,甚至试图在我的脚本中检测重复项并从那里运行不同的插入。

感谢您提供任何帮助。

+0

不可能有可能性两位艺术家有相同的名字? – nilesh

回答

-1

你如何访问你的数据库?你在使用任何种类的ORM吗?

一般而言,您可能无法再为每首曲目插入艺术家。

所以在插入你的相册时,如果艺术家已经存在,你应该查找你的表。如果存在,请取出它的id并在插入曲目时重复使用它。如果艺术家尚不存在,请插入它并保存artist_id以迭代曲目列表并使用artist_id插入每个曲目。

0

根据你的数据库设计它应该工作正常。我假装所有idsprimary key各自的表格。艺术家的名字也要约束UNIQUE。如果您使用脚本创建表格,请确保在输入后提交。现在你可以插入一个已经存在的名字。也使外键。这个示例代码可以在python3中使用sqlite。

import sqlite3 as sql 
a = sql.connect('test_db') 
conn = a.cursor() 

query = 'create table artists(id INTEGER primary key AUTOINCREMENT, artist_name varchar(30) unique)' 
conn.execute(query) 
query2 = 'create table albums(id INTEGER primary key AUTOINCREMENT, artist_id integer, foreign key(artist_id) references artists(id))' 
conn.execute(query2) 
query3 = 'create table tracks(id INTEGER primary key AUTOINCREMENT, album_name varchar(30), album_id integer, foreign key(album_id) references albums(id))' 
conn.execute(query3) 

conn.execute('insert into artists(artist_name) values("john")') 
conn.execute('insert into artists(artist_name) values("Doe")') 
conn.execute('insert into albums(artist_id) values(1)') 
conn.execute('insert into albums(artist_id) values(2)') 
conn.execute('insert into tracks(album_name, album_id) values("something",1)') 
conn.execute('insert into tracks(album_name, album_id) values("someotherthing",2)') 

a.commit() 

希望这有助于:)

+0

虽然这很有帮助,但是此查询'insert into albums(artist_id)values(1)'需要我在运行时知道artist_id,对吗?我的目标是通过autoincrement生成每个唯一条目的主键。纠正我,如果我错了,但要做到这一点,我不会首先检查艺术家是否已经存在,然后在同一个查询中使用它的主键? 这个项目的重点是监视我听​​的音乐,然后每天向这个数据库添加一次歌曲。由于我不断添加条目,因此在每个条目之前检查重复是不可避免的? – jl8n

0

为什么依赖于数据库,以防止重复的结构?
当您加载数据时,这很容易完成。
使用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