mercredi 31 décembre 2014

SQLIte ReadOnly Exception after Application has been published to Windows Phone store

I am struggling with a SQLIte Readonly exception that occurs only after I published application to WP Store.


In Development all works fine, I even checked with my real device on release ARM mode.


But after application has been certified and published and after installing app from Store, I got ReadOnly exception when I try to update the SQLite Table



[Type]:[SQLiteException]
[ExceptionMessage]:[ReadOnly]
[StackTrace]:[
at Helpers.DBHelper.Update[T](T obj, String statement)


I have used nuget libraries sqlite-net, sqlite-net-wp8 and "SQLite for Windows Phone" Visual Studio Extension


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);
}

}

Aucun commentaire:

Enregistrer un commentaire