SQLite4Unity3d使用

准备

  1. C#类
    命名空间:using SQLite4Unity3d
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>();

增删改查

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("我")));

插件地址

github地址

猜你喜欢

转载自blog.csdn.net/weixin_43796392/article/details/121291985