以下腳本在UPDATE命令中引發以下異常。我認爲這個簡單的UPDATE命令應該發生的事情是,db_2.foo.bar的值應該從1加倍到2.我的猜測是我在UPDATE語句中有一些細微的語法錯誤(或者Python中的一個錯誤或sqlite3級別);然而,我已經鑽研了sqlite3文檔 - 特別是「UPDATE」和「表達式」頁面 - 並沒有看到我做錯了什麼。Python sqlite3:更新失敗聲明「沒有這樣的列」
db_1.foo_bar= (1,)
Traceback (most recent call last):
File "try2.py", line 29, in <module>
db_2.execute('UPDATE foo SET bar = bar + db_1.foo.bar WHERE rowid = db_1.foo.rowid')
sqlite3.OperationalError: no such column: db_1.foo.bar
任何建議或解決方法?
import sqlite3
# Create db_1, populate it and close it:
open('db_1.sqlite', 'w+')
db_1 = sqlite3.connect('db_1.sqlite')
db_1.execute('CREATE TABLE foo(bar INTEGER)')
db_1.execute('INSERT INTO foo (bar) VALUES (1)')
db_1.commit()
db_1.close()
# Create db_2:
open('db_2.sqlite', 'w+')
db_2 = sqlite3.connect('db_2.sqlite')
db_2.execute('CREATE TABLE foo(bar INTEGER)')
# Attach db_1 to db_2 connection:
db_2.execute('ATTACH "db_1.sqlite" AS db_1')
# Populate db_2 from db_1:
db_2.execute('INSERT INTO foo SELECT ALL * FROM db_1.foo')
# Show that db_1.foo.bar exists:
cur_2 = db_2.cursor()
cur_2.execute('SELECT bar from db_1.foo')
for result in cur_2.fetchall():
print 'db_1.foo_bar=', result
# However, the following claims that db_1.foo.bar does not exist:
db_2.execute('UPDATE foo SET bar = bar + db_1.foo.bar WHERE rowid = db_1.foo.rowid')
db_2.execute('DETACH db_1')
db_2.commit()
db_2.close()
這似乎並非如此。雖然我找不到3.6.16文檔,但我有舊的文檔可以追溯到2005年,即使根據該文檔,腳本中UPDATE命令的語法也是有效的。 – 2010-11-13 03:33:47