zoukankan      html  css  js  c++  java
  • Xamarin android使用Sqlite做本地存储数据库

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

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

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

    下面我们就开始学习一下在Xamarin android中如何使用Sqlite呢?首先我们还是这个最终的效果图,主要的流程就是先注册添加一条用户数据,然后登陆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 如图


    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 + "
    ");
                }
                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还是挺有意思,先睡了吧

  • 相关阅读:
    人生中第一份值得纪念的工作
    ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)
    java基础之内部类
    从计算的本质到编程语言
    【Cocos2dx】资源目录,播放背景音乐,导入外部库
    POJ 3723 Tree(树链剖分)
    hdu 1002 A + B Problem II(大正整数相加)
    时间格式字符串转化为date和时间戳
    深入浅出游戏算法(4)-unity3d算法(1)-球转动
    GeoServer手动发布本地Shapefile地图
  • 原文地址:https://www.cnblogs.com/zhangmumu/p/7374788.html
Copyright © 2011-2022 走看看