I should have my SQLite
database protected by password. I found SqlCipher
but I don't understand how I can use it.
I defined my database
public class MyDatabase
{
/// <summary>
/// The database
/// </summary>
SQLiteConnection database;
/// <summary>
/// The locker
/// </summary>
static object locker = new object();
/// <summary>
/// Initializes a new instance of the <see cref="Database"/> class.
/// </summary>
public MyDatabase()
{
SetDatabase();
InitDatabase();
}
public async Task SetDatabase() {
database = await DependencyService.Get<ISQLite>().GetConnectionAsync();
}
/// <summary>
/// Init the database.
/// </summary>
public void InitDatabase()
{
if (database != null)
{
database.CreateTable<Answer>();
}
}
/// <summary>
/// Gets the items
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>All items from a table without deleted records</returns>
public List<T> GetItems<T>() where T : ITableEntity, new()
{
lock (locker)
{
List<T> rtn = null;
return (from i in database.Table<T>() select i).ToList();
}
}
/// <summary>
/// Gets the items.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="func">The function.</param>
/// <returns>List<T>.</returns>
public List<T> GetItems<T>(Expression<Func<T, bool>> func)
where T : ITableEntity, new()
{
lock (locker)
{
return database.Table<T>().Where(func).ToList();
}
}
/// <summary>
/// Gets the item.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id">The identifier.</param>
/// <returns></returns>
public T GetItem<T>(int id) where T : ITableEntity, new()
{
lock (locker)
{
return database.Table<T>().FirstOrDefault(x => x.Id == id);
}
}
/// <summary>
/// Saves the item.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="item">The item.</param>
/// <returns></returns>
public int SaveItem<T>(T item) where T : ITableEntity
{
lock (locker)
{
if (item.Id != 0)
{
database.Update(item);
return item.Id;
}
else
{
return database.Insert(item);
}
}
}
/// <summary>
/// Deletes the item.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id">The identifier.</param>
/// <returns></returns>
public int DeleteItem<T>(int id) where T : ITableEntity, new()
{
lock (locker)
{
T tmpRecord = GetItem<T>(id);
return database.Delete<T>(id);
}
}
}
Also, I defined my repository with all functions for each table. I understood I have to execute this code before any requests
var t = conn.Query<int>("PRAGMA key=xzy1921");
to set the password. All example I found use conn.Query<int>("<query>")
. How can I use that?
Thank you in advance.