Unity SQLite4Unity3d使用

准备

C#类

命名空间:using SQLite4Unity3d

1
2
3
4
5
6
7
public class TestCreateTable
{ [PrimaryKey, AutoIncrement]
PrimaryKey 下面的第一个属性作为主键
autoincrement 自增 插入数据主键会自己增大 避免主键不唯一
public int Id { get; set; }
public string Name { get; set; }
}

创建或打开数据库

1
SQLiteConnection connection=new SQLiteConnection(path, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);

插入表

1
connection.CreateTable<TestCreateTable>();

删除表

1
connection.DropTable<TestCreateTable>();

增删改查

1
2
3
TestCreateTable table = new TestCreateTable();
table.Name = "sqlite";
connection.Insert(table); //类对象插入和类同名的表中

1
2
3
4
5
6
connection.Table<TestCreateTable>() //查找表中的所有信息
Where根据条件查找<br>Where(_ => _.Id == 1) //查找id为1的行 可能有多个
First() //符合条件的第一行
 
TestCreateTable deleInfo = connection.Table<TestCreateTable>().Where(_ => _.Id == 1).First();
connection.Delete(deleInfo); //从表中删除

1
2
3
TestCreateTable updateInfo = connection.Table<TestCreateTable>().Where(_ => _.Id == 2).First();
updateInfo.Name = "我的数据变了";
connection.Update(updateInfo); 数据写入表中

1
2
3
4
5
6
7
8
9
//查找表中某一条数据
TestCreateTable searchInfo = connection.Table<TestCreateTable>().Where(_ => _.Id == 2).First();
//查找表中所有信息
List<TestCreateTable> tables = new List<TestCreateTable>(connection.Table<TestCreateTable>());
 
//插入测试数据
connection.InsertAll(new[] { new TestCreateTable() { Name="我是第一个"}, new TestCreateTable() { Name = "我是第二个" }, new TestCreateTable() { Name = "我是第三个" } });
//类似模糊查找
List<TestCreateTable> vagueInfos = new List<TestCreateTable>(connection.Table<TestCreateTable>().Where(_ => _.Name.StartsWith("我")));

  

插件地址:github地址

posted @   多见多闻  阅读(808)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示