通过从XML导出文件来创建数据库

时间:2016-06-27 14:06:28

标签: python sql xml database sqlite

我的应用程序是以XML格式读取导出文件并生成正确规范化的数据库。那么应用程序正在运行并打印结果以及存储在数据库中但在中间出现错误并且程序停止 - 注意 - DataSet是正确的格式。这是一个问题 -

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天。

1 个答案:

答案 0 :(得分:0)

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

返回None(无),因此当您尝试访问第一个元素时,会出现错误。

由于你无法确定是否总会有一个类型被返回,所以将这些东西包装成一个更简洁的东西...除了打印错误的结构,但无论如何都会继续。