2
當我嘗試使用以下語句更新記錄時遇到此異常。MethodAccessException當在sqlite db中更新記錄時
UPDATE GroupTable SET groupId=100 WHERE groupId=101
我測試了Firefox插件的SQLite Manager下的語句,它工作。 錯誤信息如下圖所示。它墜毀在os_win_c.cs
,方法名爲getTempname()
。
當我嘗試使用以下語句更新記錄時遇到此異常。MethodAccessException當在sqlite db中更新記錄時
UPDATE GroupTable SET groupId=100 WHERE groupId=101
我測試了Firefox插件的SQLite Manager下的語句,它工作。 錯誤信息如下圖所示。它墜毀在os_win_c.cs
,方法名爲getTempname()
。
那麼,我修改了原始代碼並修復了這個bug。
Path.GetTempPath()
不起作用,因爲沙箱環境。它沒有訪問權限。 我通過以下代碼修復。現在它起作用了。
static int getTempname(int nBuf, StringBuilder zBuf)
{
const string zChars = "abcdefghijklmnopqrstuvwxyz";
StringBuilder zRandom = new StringBuilder(20);
i64 iRandom = 0;
for (int i = 0; i < 20; i++)
{
sqlite3_randomness(1, ref iRandom);
zRandom.Append((char)zChars[(int)(iRandom % (zChars.Length - 1))]);
}
//! Modified by Toro, 2011,05,10
string tmpDir = "tmpDir";
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
store.CreateDirectory(tmpDir);
//zBuf.Append(Path.GetTempPath() + SQLITE_TEMP_FILE_PREFIX + zRandom.ToString());
zBuf.Append(tmpDir + "/" + SQLITE_TEMP_FILE_PREFIX + zRandom.ToString());
return SQLITE_OK;
}
上述補丁將導致在isolatedstorage一個額外的文件夾tmpDir
,而臨時文件就會因此需要自行進行刪除不會自動刪除。我試圖刪除tmpDir
中winClose
裏面os_win_c.cs
的方法中的那些文件,我發現它會在我做VACUUM
時導致崩潰。最後,我在關閉數據庫時刪除這些tmp文件。以下是SQLiteConnection
類中的Dispose方法。
public void Dispose()
{
if (_open)
{
// Original codes for close sqlite database
Sqlite3.sqlite3_close(_db);
_db = null;
_open = false;
// Clear tmp files in tmpDir, added by Toro 2011,05,13
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
string tmpDir = "tmpDir";
if (store.DirectoryExists(tmpDir) == false) return;
string searchPath = System.IO.Path.Combine(tmpDir, "*.*");
foreach (string file in store.GetFileNames(searchPath)) {
store.DeleteFile(System.IO.Path.Combine(tmpDir, file));
}
}
}