title: python sqlite not returning None even though it is None?
body: <p>This is an assignment I have to do for an online course. We are learning about data modeling and relational  databases. For this assignment, We have to take input data from an iTunes xml playlist file and put them into a database. Here is the code:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  import sqlite3
  
  conn = sqlite3.connect('trackdb14.sqlite')
  cur = conn.cursor()
  
  # Make some fresh tables using executescript()
  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 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,
      len INTEGER, rating INTEGER, count INTEGER,
      genre_id INTEGER
  );
  
  CREATE TABLE Genre (
      id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
      name    TEXT ,
      genre_id INTEGER
  
  );
  ''')
  
  
  fname =('C:/Users/Jeremiah/Desktop/Python/code3/tracks/Library.xml')
  #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 is None :
          continue
  
      #print(name, artist, album, count, rating, length,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 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 INTO Genre (name)
          VALUES ( ? )''',(genre, ) )
      cur.execute('SELECT id FROM Genre WHERE name = ? ', (genre, ))
      if (cur.fetchone()) is None or (cur.fetchone() is 'None'):
          print('Here')
      else:
          print(cur.fetchone())
      genre_id = None
  
      #print (name)
      #print(artist)
      #print(genre_id)
  
      if genre_id is not None:
  
          cur.execute('''INSERT OR IGNORE INTO Genre (genre_id)
          VALUES (?)''',(genre_id, ))
  
          cur.execute('''INSERT OR REPLACE INTO Track
              (title, album_id, len, rating, count)
              VALUES ( ?, ?, ?, ?, ? )''',
              ( name, album_id, length, rating, count ) )
          genre_id = None
          conn.commit()
          continue
  
  
      else:
  
          cur.execute('''INSERT OR REPLACE INTO Genre (genre_id)
              VALUES (?)''',(genre, ))
  
          cur.execute('''INSERT OR REPLACE INTO Track
              (title, album_id, len, rating, count)
              VALUES ( ?, ?, ?, ?, ? )''',
              ( name, album_id, length, rating, count ) )
          #genre_id = None
      conn.commit()
  print('done')
  </code></pre>
  
  <p>I need to put the <code>genre_id</code> into the track table. But when I run the code including <code>genre_id = cur.fetchone()[0]</code>, I get the error: <code>NoneType is not subscriptable</code>. So, I tried printing out <code>cur.fetchone()</code> to see which values returned <code>None</code>. I used    <code>is not None</code>    to try to find the values of <code>cur.fetchone()</code> that return <code>None</code>, but when I print out every value of <code>cur.fetchone()</code>, it still prints out <code>None</code> sometimes.</p>
  
  <p>How can I weed out the cases where <code>cur.fetchone()</code> is <code>None</code> ?</p>
  
  <p>I've been stuck for days.</p>
  
  <p>Thank you in advance for the help!!!</p>
  
  <p>Edit:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  import sqlite3
  
  conn = sqlite3.connect('trackdb14.sqlite')
  cur = conn.cursor()
  
  # Make some fresh tables using executescript()
  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 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,
      len INTEGER, rating INTEGER, count INTEGER,
      genre_id INTEGER
  );
  
  CREATE TABLE Genre (
      id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
      name    TEXT ,
      genre_id INTEGER
  
  );
  ''')
  
  
  fname =('C:/Users/Jeremiah/Desktop/Python/code3/tracks/Library.xml')
  #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 is None :
          continue
  
      #print(name, artist, album, count, rating, length,genre)
      #cur.execute('SELECT id from ')
  
      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 INTO Genre (name)
          VALUES ( ? )''',(genre, ) )
      cur.execute('SELECT id FROM Genre WHERE name = ? ', (genre, ))
      genre_id = cur.fetchone()[0]
  
      print (name)
      print(artist)
      print(genre_id)
  
      if genre_id is not None:
  
          cur.execute('''INSERT OR IGNORE INTO Genre (genre_id)
          VALUES (?)''',(genre_id, ))
  
          cur.execute('''INSERT OR REPLACE INTO Track
              (title, album_id, len, rating, count)
              VALUES ( ?, ?, ?, ?, ? )''',
              ( name, album_id, length, rating, count ) )
          genre_id = None
          conn.commit()
          continue
  
  
      else:
  
          cur.execute('''INSERT OR REPLACE INTO Genre (genre_id)
              VALUES (?)''',(genre, ))
  
          cur.execute('''INSERT OR REPLACE INTO Track
              (title, album_id, len, rating, count)
              VALUES ( ?, ?, ?, ?, ? )''',
              ( name, album_id, length, rating, count ) )
          #genre_id = None
      conn.commit()
  print('done')
  </code></pre>
  
  <p>[Command: python -u C:\Users\Jeremiah\Desktop\Python\code3\tracks\tracks.py]
  Dict count: 404
  Another One Bites The Dust
  Queen
  1
  Asche Zu Asche
  Rammstein
  3
  Beauty School Dropout
  Various
  5
  Black Dog
  Led Zeppelin
  1
  Bring The Boys Back Home
  Pink Floyd
  1
  Circles
  Bryan Lee
  11
  Comfortably Numb
  Pink Floyd
  1
  Crazy Little Thing Called Love
  Queen
  1
  Electric Funeral
  Black Sabbath
  17
  Fat Bottomed Girls
  Queen
  1
  For Those About To Rock (We Salute You)
  AC/DC
  1
  Four Sticks
  Led Zeppelin
  1
  Furious Angels
  Rob Dougan
  5
  Gelle
  Bryan Lee
  27
  Going To California
  Led Zeppelin
  1
  Grease
  Various
  5
  Hand of Doom
  Black Sabbath
  17
  Hells Bells
  AC/DC
  1
  Hey You
  Pink Floyd
  1
  I Worry
  Bryan Lee
  27
  Iron Man
  Black Sabbath
  17
  Is There Anybody Out There?
  Pink Floyd
  1
  It was a Very Good Year
  Frank Sinatra
  45
  Its Your Move
  Bryan Lee
  27
  Jack the Stripper/Fairies Wear Boots
  Black Sabbath
  17
  Killer Queen
  Queen
  1
  Laichzeit
  Rammstein
  3
  Let me Down Easy
  Bryan Lee
  27
  Misty Mountain Hop
  Led Zeppelin
  1
  No Low Down
  Bryan Lee
  27
  Now You Are Gone
  America
  45
  Outside The Wall
  Pink Floyd
  1
  Paranoid
  Black Sabbath
  17
  Planet Caravan
  Black Sabbath
  17
  Pretty Jeanie
  Bryan Lee
  27
  Rammstein
  Rammstein
  3
  Rat Salad
  Black Sabbath
  17
  Rock &amp; Roll
  Led Zeppelin
  1
  Rode Across the Desert
  America
  45
  Sandy
  Various
  5
  Shake Your Foundations
  AC/DC
  1
  Sister Golden Hair
  America
  45
  Somebody To Love
  Queen
  1
  Stairway To Heaven
  Led Zeppelin
  1
  Strangers in the Night
  Frank Sinatra
  45
  Summer Nights
  Various
  5
  Summer Wind
  Frank Sinatra
  45
  Thats Life
  Frank Sinatra
  45
  The Battle Of Evermore
  Led Zeppelin
  1
  The Blues "Is"
  Bryan Lee
  27
  There it Is
  Bryan Lee
  27
  Think
  Bryan Lee
  27
  This Town
  Frank Sinatra
  45
  Tin Man
  America
  45
  Track 01
  Billy Price
  27
  Track 02
  Billy Price
  27
  Track 03
  Billy Price
  27
  Messin with the Kid
  The Canettes Blues Band
  27
  Track 04
  Billy Price
  27
  Stormy Monday
  The Canettes Blues Band
  27
  Track 05
  Billy Price
  27
  Waiting on Ice
  Bryan Lee
  27
  War Pigs/Luke's Wall
  Black Sabbath
  17
  We Are The Champions
  Queen
  1
  We Will Rock You
  Queen
  1
  When Somebody Loves You
  Frank Sinatra
  45
  When The Levee Breaks
  Led Zeppelin
  1
  You are the One that I Want
  Various
  5
  You Done Me Wrong
  Bryan Lee
  27
  You Shook Me All Night Long
  AC/DC
  1
  You're My Best Friend
  Queen
  1
  Zion
  Fluke
  5
  Who Made Who
  AC/DC
  1
  D.T.
  AC/DC
  1
  Sink the Pink
  AC/DC
  1
  Ride On
  AC/DC
  1
  Chase the Ace
  AC/DC
  1
  Wollt Ihr Das Bett In Flammen Sehen
  Rammstein
  3
  Der Meister
  Rammstein
  3
  Weisses Fleisch
  Rammstein
  3
  Seemann
  Rammstein
  3
  Du Riechst So Gut
  Rammstein
  3
  Das Alte Leid
  Rammstein
  3
  Heirate Mich
  Rammstein
  3
  Herzeleid
  Rammstein
  3
  Baba O'Riley
  The Who
  1
  Bargain
  The Who
  1
  Love Ain't for Keeping
  The Who
  1
  My Wife
  The Who
  1
  The Song Is Over
  The Who
  1
  Getting In Tune
  The Who
  1
  Going Mobile
  The Who
  1
  Behind Blue Eyes
  The Who
  1
  Won't Get Fooled Again
  The Who
  1
  Folsom Prison Blues
  Johnny Cash
  189
  I Walk The Line
  Johnny Cash
  189
  Get Rhythm
  Johnny Cash
  189
  Big River
  Johnny Cash
  189
  Guess Things Happen That Way
  Johnny Cash
  189
  Ring Of Fire
  Johnny Cash
  189
  Jackson
  Johnny Cash
  189
  A Boy Named Sue (live)
  Johnny Cash
  189
  You Raise Me Up
  Selah
  205
  Hold On, I'm Coming
  The Canettes Blues Band
  27
  Got my Mojo Working
  The Canettes Blues Band
  27
  Sweet Home Chicago
  The Canettes Blues Band
  27
  Heavy Love
  The Canettes Blues Band
  27
  Cold Cold Feeling
  The Canettes Blues Band
  27
  Lonely Avenue
  The Canettes Blues Band
  27
  Good Morning Little Schoolgirl
  The Canettes Blues Band
  27
  Dust My Broom
  The Canettes Blues Band
  27
  Gimme' Some Lovin
  The Canettes Blues Band
  27
  Everybody Needs Somebody to Love
  The Canettes Blues Band
  27
  Black Magic Woman
  The Canettes Blues Band
  27
  Steppin' Rooster
  The Canettes Blues Band
  27
  Our Love is Drifting
  The Canettes Blues Band
  27
  Doin' 100
  The Canettes Blues Band
  27
  Mother Joy
  Matt Ender
  235
  Soldier's Lament
  Steve McDonald
  235
  Dulaman
  Altan
  235
  The Arrow
  Chris Spheeris
  235
  Spanish Eyes
  La Esperanza
  235
  Banana Bay
  Luis Villegas
  235
  Aguas De Marco
  Rosa Passos
  235
  Tamborea
  Energipsy
  235
  Gone
  Hollie Smith
  235
  The Immigrant
  Joanie Madden
  235
  Pahrump-Big Water
  Cusco
  235
  Seeker's Quest
  Cheryl Gunn
  235
  Floating To Forever
  Dean Everson
  235
  Open Road
  Jeff Bailey
  261
  Ruby
  Kaiser Chiefs
  263
  The Angry Mob
  Kaiser Chiefs
  263
  Heat Dies Down
  Kaiser Chiefs
  263
  Highroyds
  Kaiser Chiefs
  263
  Love's Not A Competition (But I'm Winning)
  Kaiser Chiefs
  263
  Thank You Very Much
  Kaiser Chiefs
  263
  I Can Do It Without You
  Kaiser Chiefs
  263
  My Kind Of Guy
  Kaiser Chiefs
  263
  Everything Is Average Nowadays
  Kaiser Chiefs
  263
  Learnt My Lesson Well
  Kaiser Chiefs
  263
  Try Your Best
  Kaiser Chiefs
  263
  Retirement
  Kaiser Chiefs
  263
  The Angry Mob [Live From Berlin]
  Kaiser Chiefs
  263
  I Like To Fight
  Kaiser Chiefs
  263
  From The Neck Down
  Kaiser Chiefs
  263
  Traceback (most recent call last):
    File "C:\Users\Jeremiah\Desktop\Python\code3\tracks\tracks.py", line 89, in 
      genre_id = cur.fetchone()[0]
  TypeError: 'NoneType' object is not subscriptable
  [Finished in 1.415s]</p>