我正在努力解决仅在我将应用程序发布到WP Store后发生的 SQLIte Readonly异常。
在开发中一切正常,我甚至在发布ARM模式时使用我的真实设备进行了检查。
但是在应用程序经过认证和发布之后以及从Store安装应用程序之后,当我尝试更新SQLite表时,我得到ReadOnly异常
[Type]:[SQLiteException]
[ExceptionMessage]:[ReadOnly]
[StackTrace]:[
at Helpers.DBHelper.Update[T](T obj, String statement)
我使用过nuget库 sqlite-net , sqlite-net-wp8 和" SQLite for Windows Phone &#34 ; Visual Studio扩展
DBHelper:
public class DBHelper : IDisposable
{
private String _dbName;
private SQLiteConnection db = null;
public DBHelper( String dbName)
{
IsolatedStorageFile store =IsolatedStorageFile.GetUserStoreForApplication();
if (!store.FileExists(dbName))
{
CopyFromContentToStorage(dbName);
}
_dbName = dbName;
}
~DBHelper()
{
Dispose(false);
}
private void Open()
{
if (db == null)
{
db = new SQLiteConnection(_dbName,SQLiteOpenFlags.ReadWrite);
}
}
private void Close()
{
if (db != null)
{
db.Close();
db.Dispose();
db = null;
}
}
//Insert operation
public int Insert<T>(T obj, string statement) where T : new()
{
try
{
Open();
SQLiteCommand cmd = db.CreateCommand(statement);
int rec = cmd.ExecuteNonQuery();
Close();
return rec;
}
catch (SQLiteException ex)
{
System.Diagnostics.Debug.WriteLine("Insert failed: " + ex.Message);
throw ex;
}
}
//Update operation
public int Update<T>(T obj, string statement) where T : new()
{
try
{
Open();
SQLiteCommand cmd = db.CreateCommand(statement);
int rec = cmd.ExecuteNonQuery();
Close();
return rec;
}
catch (SQLiteException ex)
{
System.Diagnostics.Debug.WriteLine("Update failed: " + ex.Message);
throw ex;
}
}
//Insert operation
public void Delete<T>(string statement) where T : new()
{
try
{
Open();
SQLiteCommand cmd = db.CreateCommand(statement);
cmd.ExecuteNonQuery();
Close();
}
catch (SQLiteException ex)
{
System.Diagnostics.Debug.WriteLine("Deletion failed: " + ex.Message);
throw ex;
}
}
//Query operation
//new约束指定泛型类声明中的任何类型参数都必须具有公共的无参数构造函数
public List<T> SelectList<T>(String statement) where T : new()
{
Open();
SQLiteCommand cmd = db.CreateCommand(statement);
var lst = cmd.ExecuteQuery<T>();
Close();
return lst.ToList<T>();
}
public ObservableCollection<T> SelectObservableCollection<T>(String statement)
where T : new()
{
return new ObservableCollection<T>(SelectList<T>(statement));
}
private void CopyFromContentToStorage(String dbName)
{
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
// Create a stream for the file in the installation folder.
using (Stream input = Application.GetResourceStream(new Uri(dbName, UriKind.Relative)).Stream)
{
// Create a stream for the new file in the local folder.
using (IsolatedStorageFileStream output = iso.CreateFile(dbName))
{
// Initialize the buffer.
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
// Copy the file from the installation folder to the local folder.
while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
output.Write(readBuffer, 0, bytesRead);
}
}
}
}
private static void CopyStream(System.IO.Stream input,
IsolatedStorageFileStream output)
{
byte[] buffer = new byte[32768];
long TempPos = input.Position;
int readCount;
do
{
readCount = input.Read(buffer, 0, buffer.Length);
if (readCount > 0)
{
output.Write(buffer, 0, readCount);
}
} while (readCount > 0);
input.Position = TempPos;
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
Close();
}
disposed = true;
}
}
public void Dispose() // Implement IDisposable
{
Dispose(true);
GC.SuppressFinalize(this);
}
}