我試圖在Python中使用executemany
來填充一些行。Python + sqlite3:帶有「構造」值的executemany
但是,我越來越:
sqlite3.OperationalError: 3 values for 4 columns
據我所知,在一般情況下,你使用executemany
原始值儘可能有效地填充到數據庫,並考慮到這一點,你通常需要數插入的值與列數匹配。
但是,如果一個或多個列是通過SQL「構建」的,並且沒有任何相應的輸入值呢?
小例子:
#!/usr/bin/env python2.7
import os
import sys
import sqlite3
from contextlib import closing
DB_FILE = 'test.sqlite'
with closing(sqlite3.connect(DB_FILE)) as db:
# First, some setup...
cursor = db.cursor()
cursor.execute('CREATE TABLE test (id TEXT PRIMARY KEY, a TEXT, b TEXT, c TEXT)')
sql = '''INSERT INTO test (id, a, b, c) VALUES (?, ?, ?, ?)'''
data = []
for i in range(5):
idStr = str(i)
data.append((idStr, 'a{}'.format(i), 'b{}'.format(i), 'c{}'.format(i)))
cursor.executemany(sql, data)
# Now, to exercise the problem:
# This does an 'upsert', but the same problem could occur in
# other circumstances.
sql = '''
INSERT OR REPLACE INTO test (id, a, b, c)
VALUES (
?,
(SELECT a,b FROM test WHERE id = ?),
?
)'''
data = []
for i in range(3,7):
idStr = str(i)
data.append((idStr, idStr, 'c{}'.format(i)))
cursor.executemany(sql, data)
db.commit()
當您運行的是,你在頂部(3 values for 4 columns
)中提到的錯誤。
我想我可以通過將SELECT a,b
分爲SELECT a..., SELECT b...
部分來解決這個問題,但是有沒有更好的方法?
給出的例子很少 - 實際上,它可能是10列,我現在必須用他們自己的(SELECT ...)
從句詳細列出。
更新1這是不特定於executemany
,因爲我原本以爲。即使手工運行普通的SQL也有同樣的問題。下面
更新2個的答案建議使用一個SELECT
而不是VALUES
,但是,這並在將新行被添加(如在我的「UPSERT」的情況下),因爲SELECT
沒有返回工作,沒有行案件。
注:我刪除因爲它似乎是無效的語法,所以圍繞SELECT選擇了parens。 – jwd
看起來這實際上並不起作用,如果它是一個新的ID(INSERT OR REPLACE的'INSERT'),那是對的嗎?如果是這樣,它對我的情況不起作用 – jwd