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 + "\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还是挺有意思,先睡了吧
分类:
Xamarin Android
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构