我不確定是什麼問題。它的工作原理在這裏罰款:
import sqlite3
# connect to memory-only database for testing
con = sqlite3.connect('')
cur = con.cursor()
# create the table
cur.execute('''
CREATE TABLE CorpWalletJournal (
date INT, refID INT, refTypeID INT, ownerName1 TEXT,
ownerID1 INT, ownerName2 TEXT, ownerID2 INT, argName1 TEXT,
argID1 ID, amount INT, balance INT, reason TEXT, accountKey INT,
UNIQUE (ownerID1, ownerID2, accountKey, argID1)
);
''')
con.commit()
insert_sql = '''INSERT INTO CorpWalletJournal
(date, refID, refTypeID, ownerName1, ownerID1, ownerName2, ownerID2,
argName1, argID1, amount, balance, reason, accountKey)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'''
## create 5 rows changing only argID1 - it works:
for argid in xrange(5):
cur.execute(insert_sql, (1, 1, 1, 'a', 1, 'a', 1, 'a', argid, 1, 1, 'a', 1))
con.commit()
# now try to insert a row that is already there:
cur.execute(insert_sql, (1, 1, 1, 'a', 1, 'a', 1, 'a', 0, 1, 1, 'a', 1))
我從最後一行得到的錯誤是:
Traceback (most recent call last):
File "teststdio.py", line 41, in <module>
cur.execute(insert_sql, (1, 1, 1, 'a', 1, 'a', 1, 'a', 0, 1, 1, 'a', 1))
sqlite3.IntegrityError: columns ownerID1, ownerID2, accountKey, argID1
are not unique
*它通常會在寫入2條記錄後停止,所以有些東西顯然相信它是重複的。* - 爲什麼這很明顯?插入失敗時會得到什麼錯誤信息? – 2011-01-05 13:12:51
你沒有在任何列上指定NOT NULL,所以你可能會得到空值違反唯一約束。插入失敗後數據看起來如何? – Hollister 2011-01-05 13:39:45