2016-08-16 87 views
0

以下來自類似問題的各種示例在這裏,我試圖在單聲道中使用sqlite的upsert。Sqlite單聲道upsert

string query = string.Format(@" 
        UPDATE {0} SET [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] WHERE [email protected]; 
        INSERT INTO {0} (key, data, protoent, posX, posY, posZ, ownerpubkey) VALUES (@key, @data, @protoent, @pX, @pY, @pZ, @ownerpubkey) WHERE changes() = 0; 
        ", PersistEntityRecord.TableName); 

出於某種原因,我得到一個例外,因爲它似乎並不像INSERT線

Exception thrown: 'Mono.Data.Sqlite.SqliteException' in Mono.Data.Sqlite.dll 

Additional information: SQLite error 

near "WHERE": syntax error 

的WHERE部分如果我卸下INSERT行的WHERE,它運行。我不知道爲什麼它不會那樣。

SqliteConnection.SQLiteVersion報告版本3.11.0是否有幫助

感謝

回答

0

INSERT statement有兩種形式。 WHERE子句只能使用一個SELECT查詢指定的值的形式來進行插入:

UPDATE {0} 
SET [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] 
WHERE [email protected]; 

INSERT INTO {0} (key, data, protoent, posX, posY, posZ, ownerpubkey) 
SELECT @key, @data, @protoent, @pX, @pY, @pZ, @ownerpubkey 
WHERE changes() = 0; 

可能更容易做邏輯代碼:

execute("UPDATE ..."); 
if (connection.Changes == 0) { 
    execute("INSERT ..."); 
}