Xamarin之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

[csharp] view plain copy
  1. [Table("UserInfo")]  
  2. public class UserInfo  
  3. {  
  4.     [PrimaryKey,AutoIncrement,Collation("Id")]  
  5.       public int Id { getset; }  
  6.     public string UserName { getset; }  
  7.     public string Pwd { getset; }  
  8. }  
3.MainActivity.cs 代码开单词就知道是什么意思

[html] view plain copy
  1.     public class MainActivity : Activity  
  2.     {  
  3.         int count = 1;  
  4.         private SQLiteConnection sqliteConn;  
  5.         private const string TableName = "UserInfo";  
  6.         private string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "userinfo.db3");  
  7.         private Button btnLogin;  
  8.         private Button btnRegister;  
  9.         private TextView tv_user;  
  10.         protected override void OnCreate(Bundle bundle)  
  11.         {  
  12.             base.OnCreate(bundle);  
  13.             SetContentView(Resource.Layout.Main);  
  14.             sqliteConn = new SQLiteConnection(dbPath);  
  15.             btnLogin = FindViewById<Button>(Resource.Id.btn_login);  
  16.             btnRegister = FindViewById<Button>(Resource.Id.btn_register);  
  17.             tv_user = FindViewById<TextView>(Resource.Id.tv_user);  
  18.             btnLogin.Click += delegate {  
  19.                 var userName = FindViewById<EditText>(Resource.Id.userName).Text;  
  20.                 var pwd = FindViewById<EditText>(Resource.Id.pwd).Text;  
  21.                 Login(userName,pwd);  
  22.             };  
  23.             btnRegister.Click += delegate  
  24.             {  
  25.                 var userName = FindViewById<EditText>(Resource.Id.userName).Text;  
  26.                 var pwd = FindViewById<EditText>(Resource.Id.pwd).Text;  
  27.                 Register(userName,pwd);  
  28.             };  
  29.         }  
  30.         private void Login(string  userName,string  pwd)  
  31.         {  
  32.             if (!File.Exists(dbPath))  
  33.             {  
  34.                 sqliteConn = new SQLiteConnection(dbPath);  
  35.             }  
  36.             var userInfoTable = sqliteConn.GetTableInfo(TableName);  
  37.             if (userInfoTable.Count == 0)  
  38.             {  
  39.                 sqliteConn.CreateTable<UserInfo>();  
  40.             }  
  41.             var userInfos = sqliteConn.Table<UserInfo>();  
  42.             var userInfo = userInfos.Where(p => p.Pwd == pwd && p.UserName == userName).FirstOrDefault();  
  43.             if (userInfo == null)  
  44.             {  
  45.                 Toast.MakeText(this, "用户名或密码不正确", ToastLength.Short).Show();  
  46.             }  
  47.             else  
  48.             {  
  49.                 Toast.MakeText(this, "登录成功", ToastLength.Short).Show();  
  50.             }  
  51.         }  
  52.         private void ShowUser()  
  53.         {  
  54.             if (!File.Exists(dbPath))  
  55.                 sqliteConn = new SQLiteConnection(dbPath);  
  56.   
  57.   
  58.             var userInfoTable = sqliteConn.Table<UserInfo>();  
  59.             StringBuilder sb = new StringBuilder();  
  60.             foreach (var item in userInfoTable)  
  61.             {  
  62.                  sb.Append("username:" + item.UserName + "pwd:" + item.Pwd + "\n");  
  63.             }  
  64.             tv_user.Text = sb.ToString();  
  65.         }  
  66.         private void Register(string  userName,string pwd)  
  67.         {  
  68.             if(!File.Exists(dbPath))  
  69.                 sqliteConn = new SQLiteConnection(dbPath);  
  70.            
  71.             var userInfoTable = sqliteConn.GetTableInfo(TableName);  
  72.             if (userInfoTable.Count == 0)  
  73.             {  
  74.                 sqliteConn.CreateTable<UserInfo>();  
  75.             }  
  76.             UserInfo model = new UserInfo() {Id=1UserName=userName,Pwd=pwd };  
  77.              sqliteConn.Insert(model);  
  78.             Toast.MakeText(this, "注册成功", ToastLength.Short).Show();  
  79.   
  80.   
  81.             ShowUser();  
  82.         }  
  83.     }  

现在是已经能增删改查了,但是如何查看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

转载随意注明出处
用xamarin开发android遇见 SQLite.SQLiteException: no such collation sequence: Id 解决方法
遇见错误

SQLite.SQLiteException: no such collation sequence: Id
解决方法:

[Table("UserInfo")]

public class UserInfo

{

[PrimaryKey, AutoIncrement,/* Collation("Id") 把这个暂时注释一次,然后就可以了*此为定义排序规则/]

public int Id { get; set; }

public string UserName { get; set; }

public string Pwd { get; set; }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值