Xamarin android使用Sqlite做本地存储数据库

android使用Sqlite做本地存储非常常见(打个比方就像是浏览器要做本地存储使用LocalStorage,貌似不是很恰当,大概就是这个意思)。

SQLite 是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是在世界上最广泛部署的 SQL 数据库引擎。SQLite 源代码不受版权限制。

如果不是很熟悉Sqlite,建议花点时间看看鸟巢sqlite基础教程

下面我们就开始学习一下在Xamarin android中如何使用Sqlite呢?首先我们还是这个最终的效果图,主要的流程就是先注册添加一条用户数据,然后登陆sqlite数据库,也就是做一个添加和查询。先看一下效果图:

Xamarin android使用Sqlite做本地存储数据库

这里要注意一下,Nuget上关于sqlite的xamarin类库非常多,很多国外的作者都写了sqlite的类库,所以要注意甄别,你会发现使用的方法几乎是一样的。

https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs

引入sqlite-net ,仅仅是多了SQLite.cs 和 SQLiteAsync.cs两个类库而已。比较方便的一种,可以直接看源码。

1.nuget引用sqlite-net 如图

Xamarin android使用Sqlite做本地存储数据库

2.创建一个实体UserInfo.cs

    [Table("UserInfo")]
public class UserInfo
{
[PrimaryKey,AutoIncrement,Collation("Id")]
public int Id { get; set; }
public string UserName { get; set; }
public string Pwd { get; set; }
}

3.MainActivity.cs 代码开单词就知道是什么意思

    public class MainActivity : Activity
    {
        int count = 1;
        private SQLiteConnection sqliteConn;
        private const string TableName = "UserInfo";
        private string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "userinfo.db3");
        private Button btnLogin;
        private Button btnRegister;
        private TextView tv_user;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            sqliteConn = new SQLiteConnection(dbPath);
            btnLogin = FindViewById<Button>(Resource.Id.btn_login);
            btnRegister = FindViewById<Button>(Resource.Id.btn_register);
            tv_user = FindViewById<TextView>(Resource.Id.tv_user);
            btnLogin.Click += delegate {
                var userName = FindViewById<EditText>(Resource.Id.userName).Text;
                var pwd = FindViewById<EditText>(Resource.Id.pwd).Text;
                Login(userName,pwd);
            };
            btnRegister.Click += delegate
            {
                var userName = FindViewById<EditText>(Resource.Id.userName).Text;
                var pwd = FindViewById<EditText>(Resource.Id.pwd).Text;
                Register(userName,pwd);
            };
        }
        private void Login(string  userName,string  pwd)
        {
            if (!File.Exists(dbPath))
            {
                sqliteConn = new SQLiteConnection(dbPath);
            }
            var userInfoTable = sqliteConn.GetTableInfo(TableName);
            if (userInfoTable.Count == 0)
            {
                sqliteConn.CreateTable<UserInfo>();
            }
            var userInfos = sqliteConn.Table<UserInfo>();
            var userInfo = userInfos.Where(p => p.Pwd == pwd && p.UserName == userName).FirstOrDefault();
            if (userInfo == null)
            {
                Toast.MakeText(this, "用户名或密码不正确", ToastLength.Short).Show();
            }
            else
            {
                Toast.MakeText(this, "登录成功", ToastLength.Short).Show();
            }
        }
        private void ShowUser()
        {
            if (!File.Exists(dbPath))
                sqliteConn = new SQLiteConnection(dbPath);             var userInfoTable = sqliteConn.Table<UserInfo>();
            StringBuilder sb = new StringBuilder();
            foreach (var item in userInfoTable)
            {
                 sb.Append("username:" + item.UserName + "pwd:" + item.Pwd + "\n");
            }
            tv_user.Text = sb.ToString();
        }
        private void Register(string  userName,string pwd)
        {
            if(!File.Exists(dbPath))
                sqliteConn = new SQLiteConnection(dbPath);
         
            var userInfoTable = sqliteConn.GetTableInfo(TableName);
            if (userInfoTable.Count == 0)
            {
                sqliteConn.CreateTable<UserInfo>();
            }
            UserInfo model = new UserInfo() {Id=1, UserName=userName,Pwd=pwd };
             sqliteConn.Insert(model);
            Toast.MakeText(this, "注册成功", ToastLength.Short).Show();             ShowUser();
        }
    }

现在是已经能增删改查了,但是如何查看xamarin android中sqlite的数据库呢,adb shell进入sqlite数据库即可查看

就这样吧,代码比较简单,sqlite还是挺有意思,先睡了吧。

例子下载地址:http://download.csdn.net/detail/kebi007/9718965

作者:张林

标题:amarin android使用Sqlite做本地存储数据库 原文地址:http://blog.csdn.net/kebi007/article/details/53795552

转载随意注明出处


上一篇:Scoop及使用


下一篇:xmlns:xsi ——是指xml文件遵守xml规范,xsi全名:xml schema instance