title: Parsing XML data in Python, then creating a database with this information in SQL
body: <p>I am a bit confused about the output I am getting in the command window after running my code.  I am have reviewed each section a few different times, and I am not sure what is going wrong.</p>
  
  <p>Why is the dictionary count printing, but the rest of the print statements are not showing up?  I also checked the table in the SQLite file, and none of my data is in the tables, just headings.</p>
  
  <p>Have I overlooked something?  There is no error showing up, but it is not giving the output or data in the tables, even with the insert, select, and fetchone statements.</p>
  
  <p>Thank you in advance for your comments!</p>
  
  <p>Here is what I have been playing around with:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  import sqlite3
  
  conn = sqlite3.connect('trackdb.sqlite')
  cur = conn.cursor()
  
  cur.excutescript('''
  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,
      artist_id  INTEGER,
      title   TEXT UNIQUE
  );
  
  CREATE TABLE Track (
      id  INTEGER NOT NULL PRIMARY KEY 
          AUTOINCREMENT UNIQUE,
      title TEXT  UNIQUE,
      album_id  INTEGER,
      genre_id  INTEGER,
      len INTEGER, rating INTEGER, count INTEGER
  );
  ''')   
  
  fname = raw_input('Enter file name: ')
  if ( len(fname) &lt; 1 ) : fname = 'Library.xml'
  
  # &lt;key&gt;Track ID&lt;/key&gt;&lt;integer&gt;369&lt;/integer&gt;
  # &lt;key&gt;Name&lt;/key&gt;&lt;string&gt;Another One Bites The Dust&lt;/string&gt;
  # &lt;key&gt;Artist&lt;/key&gt;&lt;string&gt;Queen&lt;/string&gt;
  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
  
  stuff = ET.parse(fname)
  all = stuff.findall('dict/dict/dict')
  print 'Dict count:', len(all)
  for entry in all:
      if ( lookup(entry, 'Track ID') is None ) : continue
  
      name = lookup(entry, 'Name')
      artist = lookup(entry, 'Artist')
      album = lookup(entry, 'Album')
      count = lookup(entry, 'Play Count')
      rating = lookup(entry, 'Rating')
      length = lookup(entry, 'Total Time')
      genre = lookup(entry, 'Genre')
  
      if name is None or artist is None or album or genre is None : continue
  
      print name, artist, album, genre, count, rating, length
  
      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 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 Genre (name)
          VALUES ( ? )''', ( genre, ) )
  
      cur.execute('SELECT id FROM Genre WHERE name = ? ', (genre, ))  
      genre_id = cur.fetchone()[0]
      cur.execute('''INSERT OR REPLACE INTO Track
          (title, album_id, genre_id, len, rating, count)
         VALUES ( ?, ?, ?, ?, ?, ? )''',
          ( name, album_id, genre_id, length, rating, count ) )
  
  conn.commit()
  </code></pre>
  
  <p>Am I missing a SELECT statement?  I tried to apply a JOIN statement to have the desired table with the track, genre, album and artists names exposed, but I can not really figure how to incorporate it.  I was getting an error when I tried genre_id = cur.fetchone()[0] If anyone can give me some insight, I would be most grateful.  </p>
  
  <p>Here is the link to the XML data:</p>
  
  <p><a href="http://www.pythonlearn.com/code/tracks.zip" rel="nofollow">http://www.pythonlearn.com/code/tracks.zip</a></p>
  
  <p>It basically shows dictionaries within dictionaries of the different key/value pairs for the track information.</p>
  
  <p>Thanks again.</p>