sqlite Python中的SQL类型错误:“NoneType”对象在流派中不可订阅

u3r8eeie  于 2023-02-23  发布在  SQLite
关注(0)|答案(3)|浏览(187)

当我运行代码时,它在“genre_id = cur.fetchone()[0]"行中显示type error,我不知道发生了什么。它对我来说很有意义,但我不知道哪里出错了。它开始做列表,但停在一个部分。我尝试改变顺序,但没有意义,我看到的解决方案基本相同,但名称不同,...它工作,但不明白为什么我的不工作

import xml.etree.ElementTree as ET
import sqlite3

conn = sqlite3.connect('trackdb.sqlite')
cur = conn.cursor()

# Make some fresh tables using executescript()
cur.executescript('''

DROP TABLE IF EXISTS Artist;
DROP TABLE IF EXISTS Genre;
DROP TABLE IF EXISTS Album;
DROP TABLE IF EXISTS Track;
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 = input('Enter file name: ')
if ( len(fname) < 1 ) : fname = 'Library.xml'

# <key>Track ID</key><integer>369</integer>
# <key>Name</key><string>Another One Bites The Dust</string>
# <key>Artist</key><string>Queen</string>
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')
    genre = lookup(entry, 'Genre')
    album = lookup(entry, 'Album')

    if name is None or artist is None or album is None :
        continue

    print(name, artist, album, genre) #cambiar aber

    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 REPLACE INTO Track
        (title, album_id, genre_id)
        VALUES ( ?, ?, ?)''',
        (name, album_id, genre_id) )

    conn.commit()
ngynwnxp

ngynwnxp1#

错误

TypeError: 'NoneType' object is not subscriptable in genre

意味着你的台词

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

未返回任何行。
您是否 * 打算 * 将元组传递给SELECT

ifsvaxew

ifsvaxew2#

您的lookup函数可以返回字符串或None。当它返回None时,您将插入NULL值。在大多数SQL实现中,包括SQLite,将任何内容与NULL进行比较都将导致NULL,这是不正确的。换句话说,您立即执行的查找id的查询将不会找到行,因为没有行满足等式“genre = NULL”。
.fetchone()的调用返回None,因为没有匹配的行,您尝试索引None,但失败了。
您需要将NULL值特殊化,如下所示:

cur.execute('''INSERT OR IGNORE INTO Genre (name)
        VALUES ( ? )''', ( genre, ) )
if genre is None:
    cur.execute('SELECT id FROM Genre WHERE name IS NULL ')
else:
    cur.execute('SELECT id FROM Genre WHERE name = ? ', (genre, ))
genre_id = cur.fetchone()[0]
jogvjijk

jogvjijk3#

问题在于在SQL中你不能使用“None”类型值,所以当你的Python程序解析xml文件中的数据时,你会得到一个“None”类型的体裁值,如果你执行以下操作,你就可以修复这个问题:
如果名称为“无”、艺术家为“无”、专辑为“无”或流派为“无”:继续

相关问题