Forms Xamarin、SQLite和加密(SQLCipher)
我应该让我的Forms Xamarin、SQLite和加密(SQLCipher),forms,sqlite,xamarin,sqlcipher,Forms,Sqlite,Xamarin,Sqlcipher,我应该让我的SQLite数据库受密码保护。我找到了SqlCipher,但我不知道如何使用它 我定义了我的数据库 public class MyDatabase { /// <summary> /// The database /// </summary> SQLiteConnection database; /// <summary> /// The locker /// </summary>
SQLite
数据库受密码保护。我找到了SqlCipher
,但我不知道如何使用它
我定义了我的数据库
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);
}
}
}
公共类数据库
{
///
///数据库
///
SQLiteConnection数据库;
///
///储物柜
///
静态对象锁定器=新对象();
///
///初始化类的新实例。
///
公共数据库()
{
SetDatabase();
InitDatabase();
}
公共异步任务SetDatabase(){
数据库=等待DependencyService.Get().GetConnectionAsync();
}
///
///初始化数据库。
///
公共数据库()
{
if(数据库!=null)
{
CreateTable();
}
}
///
///获取项目
///
///
///表中未删除记录的所有项目
public List GetItems(),其中T:ITableEntity,new()
{
锁(储物柜)
{
列表rtn=null;
返回(从database.Table()中的i选择i.ToList();
}
}
///
///获取项目。
///
///
///功能。
///利斯特。
公共列表GetItems(表达式函数)
其中T:ITableEntity,new()
{
锁(储物柜)
{
返回database.Table().Where(func.ToList();
}
}
///
///获取该项。
///
///
///标识符。
///
public T GetItem(int-id),其中T:ITableEntity,new()
{
锁(储物柜)
{
返回database.Table().FirstOrDefault(x=>x.Id==Id);
}
}
///
///保存项目。
///
///
///这个项目。
///
公共int保存项(T项),其中T:ITableEntity
{
锁(储物柜)
{
如果(item.Id!=0)
{
数据库更新(项目);
返回项目.Id;
}
其他的
{
返回数据库。插入(项);
}
}
}
///
///删除该项。
///
///
///标识符。
///
公共int-DeleteItem(int-id),其中T:ITableEntity,new()
{
锁(储物柜)
{
T tmpRecord=GetItem(id);
返回数据库。删除(id);
}
}
}
此外,我还为每个表定义了包含所有函数的存储库。
我知道我必须在任何请求之前执行此代码
var t = conn.Query<int>("PRAGMA key=xzy1921");
var t=conn.Query(“PRAGMA key=xzy1921”);
设置密码。我找到的所有示例都使用conn.Query(“”
)。我怎么用这个
先谢谢你