import sqlite3 import os import pyexcel_odsr os.remove('songbook.sqlite') db = sqlite3.connect("songbook.sqlite") cur = db.cursor() with open('db.sql') as file: db.executescript(file.read()) data = pyexcel_odsr.get_data('reference.ods') def nullifempty(cell): return None if len(cell)==0 else cell # import song cur.executemany( 'insert or ignore into song(id,name,name_jp) values(?, ?, ?)', ((row[0],row[3],None if len(row)<5 else nullifempty(row[4])) for row in data['Song'][1:]) ) # import album cur.executemany( 'insert into album(id,name,date,code) values (?, ?, ?, ?)', data['Album'][1:] ) # import artist cur.executemany( 'insert into artist(id,name_rm,name) values(?, ?, ?)', ((row[0],nullifempty(row[1]),row[2]) for row in data['Artist'][1:]) ) # import credit cur.executemany( 'insert into credit(id,name) values(?, ?)', data['Credit'][1:] ) # import motif cur.executemany( 'insert into motif(id,name) values(?, ?)', data['Motif'][1:] ) # import clip cur.executemany( 'insert into clip(start_ms,duration_ms,song_id,motif_id) values (?, ?, ?, ?)', data['Clip'][1:] ) # import song x artist def creditparams(rows): for row in rows[1:]: for index,artist in enumerate(row[5:], 5): if len(artist)>0: yield (row[0],artist,rows[0][index]) cur.executemany( 'insert or ignore into song_artist(song_id,artist_id,credit_id) values (?, (select id from artist where coalesce(name_rm,name) = ?), (select id from credit where name = ?))', creditparams(data['Song']) ) # import song x album # "insert into song_album(album_id,track,song_id) values (?, ?, (select id from song where name = ? and coalesce(name_jp,'') = coalesce(?,'')))" cur.executemany( "insert into song_album(song_id,album_id,track) values (?, ?, ?)", ((row[0],row[1],row[2]) for row in data['Song'][1:]) ) db.commit()