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( "我" ))); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!