Skip to content

Instantly share code, notes, and snippets.

@adamped
Created January 8, 2016 01:47
Show Gist options
  • Save adamped/5d3f8ac8c48810379b1a to your computer and use it in GitHub Desktop.
Save adamped/5d3f8ac8c48810379b1a to your computer and use it in GitHub Desktop.
SQLite for WinPhone
public class SQLite_WinPhone : ISQLite
{
private static SQLiteConnectionWithLock _conn;
public SQLite_WinPhone() { }
private static Object _connectionLock = new Object();
private string DatabaseName = "DatabaseName.db3";
public SQLite.Net.Async.SQLiteAsyncConnection GetAsyncConnection()
{
lock (_connectionLock)
{
var sqliteFilename = DatabaseName;
string path = Path.Combine(ApplicationData.Current.LocalFolder.Path, sqliteFilename);
var platform = new SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8();
var connectionString = new SQLiteConnectionString(path, storeDateTimeAsTicks: false);
var connectionFactory = new Func<SQLiteConnectionWithLock>(
() =>
{
if (_conn == null)
{
_conn =
new SQLiteConnectionWithLock(platform,
connectionString);
}
return _conn;
});
return new SQLiteAsyncConnection(connectionFactory);
}
}
public void DeleteDatabase()
{
try
{
string path = Path.Combine(ApplicationData.Current.LocalFolder.Path, DatabaseName);
CloseConnection();
if (File.Exists(path))
{
File.Delete(path);
}
}
catch
{
throw;
}
}
public void CloseConnection()
{
if (_conn != null)
{
_conn.Close();
_conn.Dispose();
_conn = null;
// Must be called as the disposal of the connection is not released until the GC runs.
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment