xamarin.Android SQLite存储

在可移植类库 新建:

using SQLite.Net.Interop;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonLib
{
    /// <summary>
    /// 数据存储环境
    /// </summary>
    public interface IDataStorage
    {
        /// <summary>
        /// 存储路径
        /// </summary>
        string StoragePath { get; }
        /// <summary>
        ///SQLite 平台环境
        ///     IOS: SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS
        /// Android: SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid
        ///...
        /// </summary>
        ISQLitePlatform SqlitePlatform { get; }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SQLite.Net;

namespace CommonLib
{
    /// <summary>
    /// SQLite上下文
    /// </summary>
    public class SQLiteContext
    {
        public SQLiteContext() { }

        private IDataStorage _dataStorage;

        /// <summary>
        /// 构造上下文
        /// </summary>
        /// <param name="dataStorage">数据存储环境</param>
        public SQLiteContext(IDataStorage dataStorage)
        {
            _dataStorage = dataStorage;
        }

        /// <summary>
        /// SQLite上下文
        /// </summary>
        protected SQLiteContext Connection {
            get {
                return this;
            }
        }

        /// <summary>
        /// 创建链接
        /// </summary>
        /// <returns></returns>
        public SQLiteConnection New()
        {
            if (_dataStorage == null)
                throw new ArgumentException("SQLiteContext构造时缺少IDataStorage对象实例!");

            return new SQLite.Net.SQLiteConnection(_dataStorage.SqlitePlatform, _dataStorage.StoragePath);
        }
    }
}

 

在xamarin.Android 工程内新建 SQLiteStorage.cs

 /// <summary>
    /// 数据存储环境
    /// </summary>
    public class SQLiteStorage : IDataStorage
    {
        /// <summary>
        /// 存储路径
        /// </summary>
        public string StoragePath
        {
            get
            {
                // 获取数据库文件存储路径
                return System.IO.Path.Combine(
                    System.Environment.GetFolderPath(
                        System.Environment.SpecialFolder.Personal), "db.db");
            }
        }

        /// <summary>
        /// SQLite 平台环境
        /// Android: SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid
        ///     IOS: SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS
        ///     ...
        /// </summary>
        public SQLite.Net.Interop.ISQLitePlatform SqlitePlatform
        {
            get
            {
                return new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
            }
        }
    }

 使用例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SQLite.Net;
using CommonLib;

namespace BusinessLib
{
    /// <summary>
    ///  涉及到操作SQLite时需要继承 ZsCMS.CommonLib.SQLiteContext
    /// </summary>
    public class TestSqlte: SQLiteContext
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataStorage">数据存储环境</param>
        public TestSqlte(IDataStorage dataStorage)
            : base(dataStorage)// 将UI层平台信息传入父类.创建当前平台的SQLite上下文
        { }

        public void Insert(ZsCMS.ModelsLib.TestSqlte model)
        {
            // 通过父类Connection 创建数据链接
            using (var conn= Connection.New())
            {
                // 当表存在不会被创建,不存在表则创建
                conn.CreateTable<ZsCMS.ModelsLib.TestSqlte>();
                
                // 往表中添加数据
                conn.Insert(model);
            }
        }

        public IEnumerable<ZsCMS.ModelsLib.TestSqlte> Gets()
        {
            using (var conn = Connection.New())
            {
                // 查询数据列表
                return conn.Query<ZsCMS.ModelsLib.TestSqlte>("select * from TestSqlte");
            }
        }
    }
}

 

    原文作者:sqlite
    原文地址: https://www.cnblogs.com/mycing/p/5557027.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞