2012-04-02 62 views
0

我在Windows x64 Visual Studio 2010中使用Sqlite 3.7.11。下面的示例可以從sqlite3.exe工具運行。我從代碼中得到同樣的失敗。Sqlite拒絕引用的值爲不明確的列名

對於這個表:

PRAGMA foreign_keys=ON; 

CREATE TABLE hashes(id INTEGER PRIMARY KEY, hash CHARACTER(4) NOT NULL UNIQUE); 
CREATE TABLE sources(id INTEGER PRIMARY KEY, source VARCHAR(64) NOT NULL UNIQUE); 
CREATE TABLE files(hash_id INTEGER, source_id INTEGER, filename VARCHAR(2048), extension VARCHAR(16), 
FOREIGN KEY(hash_id) REFERENCES hashes(id) 
FOREIGN KEY(source_id) REFERENCES sources(id) 
UNIQUE(hash_id, source_id, filename)); 

而這個初始數據:

INSERT INTO hashes VALUES(?, "abcd"); 
INSERT INTO sources VALUES(?, "mysource"); 

我加入這樣的行。文件表根據上述約束條件引用散列表和源表。

INSERT INTO files (hash_id, source_id, filename, extension) 
SELECT hashes.id, sources.id, "filename.ext", "ext" 
FROM hashes, sources 
WHERE hashes.hash = "abcd" AND sources.source = "mysource"; 

一切工作正常,直到'id'出現在文件名或擴展名字段。這沒有任何意義 - 它被引用。這兩個刀片將失敗:

INSERT INTO files (hash_id, source_id, filename, extension) 
SELECT hashes.id, sources.id, "id", "" 
FROM hashes, sources 
WHERE hashes.hash = "abcd" AND sources.source = "mysource"; 

INSERT INTO files (hash_id, source_id, filename, extension) 
SELECT hashes.id, sources.id, "filename.id", "id" 
FROM hashes, sources 
WHERE hashes.hash = "abcd" AND sources.source = "mysource"; 

錯誤:不明確的列名:ID

我需要以某種方式進一步逃脫引用價值?

+0

最後一個「id」屬於哪裏?我的意思是,哪一列是「身份證」? – Panagiotis 2012-04-02 17:12:08

+0

引用的「id」是第一個失敗示例中的文件名,第二個失敗示例中的擴展名。插入後,該列應包含文字字符串「id」。 – bolts 2012-04-02 17:19:02

+0

不應該在where子句中(filename LIKE「filename.id」AND extension LIKE「id」)? – Panagiotis 2012-04-02 17:21:35

回答

1

呃...那是一個菜鳥的錯誤。一旦我使用單引號指示字符串文字時,此工作原理如下:

INSERT INTO files (hash_id, source_id, filename, extension) 
SELECT hashes.id, sources.id, "id", "" 
FROM hashes, sources 
WHERE hashes.hash = "abcd" AND sources.source = "mysource"; 

INSERT INTO files (hash_id, source_id, filename, extension) 
SELECT hashes.id, sources.id, 'filename.id', 'id' 
FROM hashes, sources 
WHERE hashes.hash = "abcd" AND sources.source = "mysource";