Unity SQLite4Unity3d使用

准备

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

  

插件地址:github地址

posted @ 2022-02-16 11:42  多见多闻  阅读(940)  评论(0)    收藏  举报