sqlite3.OperationalError:没有这样的列。但那不是专栏

时间:2012-08-06 00:07:15

标签: python sqlite python-2.7

我似乎得到了一个相当奇怪的错误。而且,对于我的生活,我无法弄清楚出了什么问题。但是在一段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()

1 个答案:

答案 0 :(得分:12)

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

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

[编辑:&#34;%s&#34;不是正确的语法。它应该是&#34;?&#34;。此外,如果您将成为居高临下的人,至少应该是正确的。]