2

我似乎遇到了一个相当奇怪的错误。而且,对于我的生活,我无法弄清楚出了什么问题。但是在一段 SQLite 代码上,我收到了这个错误:

Traceback (most recent call last):
  File "test.py", line 38, in <module>
    populateTables()
  File "test.py", line 20, in populateTables
    curs.execute("SELECT * FROM tracks WHERE ISRC = " + line[8])
sqlite3.OperationalError: no such column: USTCZ0993316

奇怪的是,USTCZ0993316 是我想比较的一条数据。我不知道为什么它似乎认为它是一个专栏。这是一个非常小的版本,它给出了同样的问题。

import sqlite3
import csv

def tableSetup(name):
    if(name=="tracks"):
        curs.execute("CREATE TABLE tracks(id INT UNIQUE, name TINYTEXT, album_id INT, client_id INT, acr_record_num INT, ISRC TINYTEXT UNIQUE, track_length TINYTEXT, client_share FLOAT)")

def populateTables():
    tracks_csv=csv.reader(open('tables/tracks.csv', 'rU'), delimiter=";", quotechar='"')
    tracks_csv.next()
    for line in tracks_csv:
        curs.execute("SELECT * FROM tracks WHERE id = " + line[0])
        if not curs.fetchall():
            if "\"" in line[1]:
                line[1]=line[1].replace("\"","'")
            curs.execute("INSERT INTO tracks VALUES("+line[0]+",\""+line[1]+"\","+line[2]+","+line[3]+","+line[4]+",\""+line[5]+"\",\""+line[7]+"\","+line[12]+")")
    override_csv=csv.reader(open('tables/artist_override.csv', 'rU'), delimiter=",", quotechar='"')
    override_csv.next()
    for line in override_csv:
        curs.execute("SELECT * FROM tracks WHERE ISRC = " + line[8])
        print curs.fetchone()

#Set required Table Names
tables = ["tracks"]
testOut=open('tables/testOut.txt','w')
conn = sqlite3.connect('tables/test.db')

curs = conn.cursor()

# Create table if they don't already exist
curs.execute("SELECT name FROM sqlite_master WHERE type='table'")
tableResults = curs.fetchall()
print
for table in tables:
    if not any(table == result[0] for result in tableResults):
        tableSetup(table)

populateTables()
conn.commit()
curs.close()
4

1 回答 1

13

如果它没有引号,那么它是一个列或一个数字。

curs.execute("SELECT * FROM tracks WHERE ISRC = ?", (line[8],))

[编辑:“%s”不是正确的语法。它应该是 ”?”。另外,如果您要居高临下,至少要正确。]

于 2012-08-06T00:09:10.403 回答