Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL and fetchone
#1
hello,

I don't understand the following error. you Ould find under my code and the file I try to enter in a database.
import xml.etree.ElementTree as ET
import sqlite3

conn = sqlite3.connect('trackdb.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 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
);

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


fname = input('Enter file name: ')
if ( len(fname) < 1 ) : fname = 'Library.xml'


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')
    genre = lookup(entry, 'Genre')
    count = lookup(entry, 'Play Count')
    rating = lookup(entry, 'Rating')
    length = lookup(entry, 'Total Time')

    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 OR IGNORE INTO Genre (genre)
        VALUES (?)''', (genre, ))
    cur.execute('SELECT id FROM Genre WHERE genre = ? ', (genre, ))
    print("=======\n", cur.fetchone()[0], genre)
    genre_id = cur.fetchone()[0]

    cur.execute('''INSERT OR REPLACE INTO Track
        (title, album_id, len, rating, count, genre_id) 
        VALUES ( ?, ?, ?, ?, ?, ?)''',
        (name, album_id, length, rating, count, genre_id))

    conn.commit()
Error:
Enter file name: Library.xml Dict count: 404 Another One Bites The Dust Queen Greatest Hits 55 100 217103 Rock ======= 1 Rock Traceback (most recent call last): File "/Users/nic/Desktop/coursera/tracks/tracks2.py", line 86, in <module> genre_id = cur.fetchone()[0] TypeError: 'NoneType' object is not subscriptable Process terminated with an exit code of 1


for the first tables, my code is running but for the table "genre" is blowed up
why the same code using fetchone doesn't return the same result?

thanks for your help
Reply


Messages In This Thread
SQL and fetchone - by Scorpio - Feb-13-2019, 12:52 PM
RE: SQL and fetchone - by buran - Feb-13-2019, 01:37 PM
RE: SQL and fetchone - by Scorpio - Feb-13-2019, 04:54 PM
RE: SQL and fetchone - by buran - Feb-13-2019, 05:07 PM
RE: SQL and fetchone - by Scorpio - Feb-13-2019, 06:35 PM
RE: SQL and fetchone - by beebert69 - Oct-14-2020, 02:24 PM
RE: SQL and fetchone - by buran - Feb-13-2019, 06:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert SQLite Fetchone() Result to float for Math Extra 13 4,122 Aug-02-2022, 01:12 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020