如何以Xamarin形式使用SQLite-Net-PCL? [英] How to use SQLite-Net-PCL in xamarin forms?

查看:378
本文介绍了如何以Xamarin形式使用SQLite-Net-PCL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在项目中安装了SQLite-net-pcl软件包.现在,我想将其用于简单的CRUD操作.事实是,我阅读的所有文档都使我感到困惑.有人可以提示我在Xamarin.Forms中执行CRUD操作以接受值表单Entry并将其存储在数据库中的适当步骤吗?

I have installed SQLite-net-pcl package in my project. And now I want to use it for simple CRUD operations. The thing is that all the documentation I read was confusing to me. Can anyone hint me for the proper steps to perform a CRUD operation in Xamarin.Forms to accept a value form Entry and store it in database?

推荐答案

我在工作的应用中使用此方法.

I use this method in my working app.

  1. 安装 SQLite-net-pcl

我使用异步方法.为了排除锁,我使用了此类:

I use async methods. To exclude locks, I use this class:

public sealed class AsyncLock
{
  private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  private readonly Task<IDisposable> _releaser;

public AsyncLock()
{
    _releaser = Task.FromResult((IDisposable)new Releaser(this));
}

public Task<IDisposable> LockAsync()
{
    var wait = _semaphore.WaitAsync();
    return wait.IsCompleted ?
        _releaser :
        wait.ContinueWith((_, state) => (IDisposable)state,
        _releaser.Result, CancellationToken.None,
        TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}

private sealed class Releaser : IDisposable
{
    private readonly AsyncLock m_toRelease;

    internal Releaser(AsyncLock toRelease)
    {
        m_toRelease = toRelease;
    }

    public void Dispose()
    {
        m_toRelease._semaphore.Release();
    }
}
}

  • 创建域(表):

  • Create domains(tables):

     //base class for table  
     public class Entity
     {
       [PrimaryKey, AutoIncrement]
       public int Id { get; set; }
     }  
    
     //your table
     public class Data :Entity
     {
        public string Prop1 {get;set;}  
        ......   
     }
    
     public class Data2 :Entity
     {
        public string Prop2 {get;set;}  
        ......   
     } 
    

  • 创建存储库:

  • Create repository:

    public class DataRepository
    {
      private SQLiteAsyncConnection _db;
      private static readonly AsyncLock Mutex = new AsyncLock();
    
      public async Task CreateDatabaseAsync(string path)
      {
        using (await Mutex.LockAsync().ConfigureAwait(false))
        {
           _db= new SQLiteAsyncConnection(path);
           await _db.CreateTableAsync<Data>();
           //create other tables
        }
    
        public async Task Save<T>(T entity) where T : Entity, new()
        {
          using (await Mutex.LockAsync().ConfigureAwait(false))
          {
            await _db.InsertAsync(entity);
          }
         }
    
         public async Task Delete(Entity item) 
         {
           using (await Mutex.LockAsync().ConfigureAwait(false))
           {
            await _db.DeleteAsync(item);
           }
         } 
    
         public async Task Update<T>(T entity) where T : Entity, new()
         {
            using (await Mutex.LockAsync().ConfigureAwait(false))
            {
              await _db.UpdateAsync(entity);
            }
         }           
         ........
         //other base method
      }
    

    1. 在您的App类中为DataRepository创建静态字段.用这个 App.Repo输入您的代码.

    1. Create static field for DataRepository in your App class. Use this App.Repo in your code.

       App.Repo.Save(new Data
                {
                   ...
                }) ;
    

  • 这是使用的简化示例.

    这篇关于如何以Xamarin形式使用SQLite-Net-PCL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆