2016-06-27 4 views
1

私のアプリケーションは、XMLでエクスポートファイルを読み込み、適切に正規化されたデータベースを生成することです。まあ、アプリケーションが実行され、結果が印刷され、データベースに格納されますが、途中でエラーが出てプログラムが停止します。 注 - DataSetは適切な形式です。それは教室です質問 -XMLからファイルをエクスポートしてデータベースを作成する

I Like To Fight Kaiser Chiefs Yours Truly, Angry Mob 15 218566 None Alternative & Punk 
From The Neck Down Kaiser Chiefs Yours Truly, Angry Mob 15 147226 None Alternative & Punk 
Bomb Squad (TECH) Brent Brent's Album None 208065 None None  
Traceback (most recent call last): 
     File "C:\Users\Dhruv\misctrack.py", line 75, in <module> 
     genre_id= cur.fetchone()[0] 
    TypeError: 'NoneType' object has no attribute '__getitem__' 

以下は完全なプログラムです。私は前回提案されたので、完全なプログラムを与えられました。不適切な場合はお知らせください。

import sqlite3 
import xml.etree.ElementTree as ET 
conn=sqlite3.connect('misctrackdb.sqlite') 
cur=conn.cursor() 

cur.executescript(''' 
DROP TABLE IF EXISTS Artist; 
DROP TABLE IF EXISTS Album; 
DROP TABLE IF EXISTS Track; 
DROP TABLE IF EXISTS Genre; 

CREATE TABLE Artist(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 
name TEXT UNIQUE 
); 

CREATE TABLE Genre(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 
name TEXT UNIQUE 
); 

CREATE TABLE Album(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 
title TEXT UNIQUE, 
artist_id INTEGER 
); 

CREATE TABLE Track (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 
title TEXT UNIQUE, 
genre_id INTEGER, 
album_id INTEGER, 
len INTEGER,rating INTEGER, count INTEGER 
); 

''') 
def lookup(d, key): 
    found = False 
    for child in d: 
     if found : return child.text 
     if child.tag == 'key' and child.text == key : 
      found = True 
    return None 


fle=raw_input("Enter File HEre") 
stuff=ET.parse(fle) 
all=stuff.findall('dict/dict/dict') 
print 'Dict count:', len(all) 

for line in all: 
    if lookup(line,'Track ID') is None: 
     continue 
    #print "1" 
    name=lookup(line,'Name') 
    album=lookup(line,'Album') 
    genre=lookup(line,'Genre') 
    artist=lookup(line,'Artist') 
    count=lookup(line,'Track Count') 
    len=lookup(line,'Total Time') 
    rating=lookup(line,'Rating') 
    #print "2" 

    if name is None or artist is None or album is None : 
     continue 

    print name, artist ,album ,count ,len, rating, genre 

    cur.execute(''' INSERT OR IGNORE INTO Artist (name) 
    VALUES(?)''',(artist,)) 
    cur.execute('SELECT id FROM Artist WHERE name =?',(artist,)) 
    artist_id= cur.fetchone()[0] 

    cur.execute('''INSERT OR IGNORE INTO Genre (name) 
    VALUES(?)''',(genre,)) 
    cur.execute('SELECT id FROM Genre WHERE name=?',(genre,)) 
    genre_id= cur.fetchone()[0] 

    cur.execute('''INSERT OR IGNORE INTO Album (title, artist_id) 
    VALUES(?,?)''',(album,artist_id)) 
    cur.execute('SELECT id FROM Album WHERE title = ?',(album,)) 
    album_id=cur.fetchone()[0] 

    cur.execute('''INSERT OR IGNORE INTO Track (title,genre_id,album_id,len,rating,count) 
    VALUES(?,?,?,?,?,?)''',(album,genre_id,album_id,len,rating,count)) 
    conn.commit() 

問題のソートに手伝ってください。私はこの問題で2日間立ち往生しました。

答えて

0
cur.execute('SELECT id FROM Genre WHERE name=?',(genre,)) 

戻り値なし(何も)、あなたが最初の要素をにアクセスしようとするので、あなたがエラーを取得します。

返されるジャンルは常に存在するとは確信できませんので、エラーを出力するtry .. except構造体にこれらの要素をラップするときれいになりますが、とにかく続行します。

関連する問題