SQLite4Unity3d使用
数据库准备C#类命名空间:using SQLite4Unity3dpublic class TestCreateTable{[PrimaryKey, AutoIncrement]PrimaryKey 下面的第一个属性作为主键autoincrement 自增 插入数据主键会自己增大 避免主键不唯一public int Id { get; set; }public string Name { get;
·
准备
- C#类
命名空间:using SQLite4Unity3d
public class TestCreateTable
{ [PrimaryKey, AutoIncrement]
PrimaryKey 下面的第一个属性作为主键
autoincrement 自增 插入数据主键会自己增大 避免主键不唯一
public int Id { get; set; }
public string Name { get; set; }
}
- 创建或打开数据库
SQLiteConnection connection=new SQLiteConnection(path, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
- 插入表
connection.CreateTable<TestCreateTable>();
- 删除表
connection.DropTable<TestCreateTable>();
增删改查
- 增
TestCreateTable table = new TestCreateTable();
table.Name = "sqlite";
connection.Insert(table); 类对象插入和类同名的表中
- 删
connection.Table<TestCreateTable>() 查找表中的所有信息
Where根据条件查找Where(_ => _.Id == 1) 查找id为1的行 可能有多个
First() 符合条件的第一行
TestCreateTable deleInfo = connection.Table<TestCreateTable>().Where(_ => _.Id == 1).First();
connection.Delete(deleInfo); 从表中删除
- 改
TestCreateTable updateInfo = connection.Table<TestCreateTable>().Where(_ => _.Id == 2).First();
updateInfo.Name = "我的数据变了";
connection.Update(updateInfo); 数据写入表中
- 查
查找表中某一条数据
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("我")));
插件地址
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)