码迷,mamicode.com
首页 > 移动开发 > 详细

xamarin.Android SQLite存储

时间:2016-06-03 18:43:30      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

在可移植类库 新建:

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");
            }
        }
    }
}

 

xamarin.Android SQLite存储

标签:

原文地址:http://www.cnblogs.com/mycing/p/5557027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!