My application is to read an export file in XML and produce a properly normalized database. Well The application is running and printing results as well as storing in database but in the middle an error pops out and the program halts- Note- DataSet is proper format. It is coursera Question-
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__'
Following is the complete program. I am given complete program because I was suggested last time. If it's inappropriate please inform.
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()
Please help me in sorting out the issue. I have been stuck for 2 days in this issue.
cur.execute('SELECT id FROM Genre WHERE name=?',(genre,))
Returns None (nothing), so when you try to to access the first element, you get your error.
Since you cannot be sure that there will always be a genre to be returned, it would be cleaner to wrap these things into a try.. except structure which prints the error, but continues anyway.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.