当前位置:网站首页>Unity 使用Sqlite
Unity 使用Sqlite
2022-07-01 05:22:00 【zLulus】
我看网上都在讲引用sqlite3.dll,Mono.Data.Sqlite.dll,System.Data.dll,比如Setup Database (SQLite) for Unity
或者第三方的一些导入方式unity-3rdparty-sqlite-net
或者通过第三方包管理工具导入Unity3D 入门:如何管理 Unity 项目中的 NuGet 包?使用第三方 NuGet 包管理器——NuGetForUnity
但我发现Unity有关于Sqlite的库Unity.VisualScripting.Dependencies.Sqlite.SQLiteConnection,资料有Unity.VisualScripting.Dependencies.Sqlite.SQLiteConnection
所以我就简单做了个增删改查的demo,Unity.VisualScripting.Dependencies.Sqlite.SQLiteConnection可以直接使用,可以完成对Sqlite的操作
下面是关键代码
创建表
using (var db = new SQLiteConnection(databasePath, SQLiteOpenFlags.ReadWrite))
{
var count = db.CreateTable<UserInfo>();
count = db.CreateTable<RandomEvent>();
}
增加数据
using (var db = new SQLiteConnection(databasePath, SQLiteOpenFlags.ReadWrite))
{
var count = db.Insert(new UserInfo()
{
Name = "test1Name",
Sex = SexEnum.Woman,
Birthday = new DateTime(1990, 10, 2)
}, typeof(UserInfo));
List<UserInfo> users = new List<UserInfo>();
users.Add(new UserInfo()
{
Name = "test2Name",
Sex = SexEnum.Man,
Birthday = new DateTime(2000, 10, 2)
});
users.Add(new UserInfo()
{
Name = "test3Name",
Sex = SexEnum.Woman,
Birthday = new DateTime(2004, 10, 2)
});
count = db.InsertAll(users, typeof(UserInfo));
}
修改数据
using (var db = new SQLiteConnection(databasePath, SQLiteOpenFlags.ReadWrite))
{
var count = db.Update(new UserInfo()
{
Id = 3,
Name = "test1Name-Update",
Sex = SexEnum.Woman,
Birthday = new DateTime(1990, 10, 2)
}, typeof(UserInfo));
}
删除数据
using (var db = new SQLiteConnection(databasePath, SQLiteOpenFlags.ReadWrite))
{
string sql = "select Id,Name,Sex,Birthday from UserInfo where Id =3";
var data = new object[] { "Id", "Name", "Sex", "Birthday" };
var deleteData = db.Query(new TableMapping(typeof(UserInfo)), sql, data).FirstOrDefault();
if (deleteData != null)
{
var count = db.Delete(deleteData);
}
var count2 = db.Delete<UserInfo>(4);
}
查询数据
using (var db = new SQLiteConnection(databasePath, SQLiteOpenFlags.ReadWrite))
{
string sql = "select Id,Question,[Order] from RandomEvent";
var data = new object[] { "Id", "Question", "[Order]" };
var datas = db.Query(new TableMapping(typeof(RandomEvent)), sql, data);
//enum not work :(
//var datas2 = db.Find<UserInfo>(user => user.Name.Contains("test"));
var datas2 = db.Find<RandomEvent>(randomEvent => randomEvent.Question.Contains("q"));
var data3 = db.Get<UserInfo>(1);
}
数据表需要做声明
[Table(name: nameof(RandomEvent))]
public class RandomEvent
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Question { get; set; }
public int Order { get; set; }
}
[Table(name: nameof(UserInfo))]
public class UserInfo
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public SexEnum Sex { get; set; }
public DateTime Birthday { get; set; }
}
示例代码
TestSqliteScript
打开项目后,在TestSqliteCanvas下即可执行
如果觉得有不合适的地方,可以自己再封装一层~
边栏推荐
- Causes of short circuit of conductive slip ring and Countermeasures
- Go learning notes (5) basic types and declarations (4)
- Numeric amount plus comma; JS two methods of adding three digits and a comma to numbers; JS data formatting
- 提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
- Actual combat: basic use of Redux
- Global and Chinese markets of Ethernet communication modules 2022-2028: Research Report on technology, participants, trends, market size and share
- Distributed architecture system splitting principles, requirements and microservice splitting steps
- Flutter 实现每次进来界面都刷新数据
- Web Security (x) what is OAuth 2.0?
- Copy baby prompt: material cannot be empty. How to solve it?
猜你喜欢

Txncoordsender of cockroachdb distributed transaction source code analysis

How to meet the requirements of source code confidentiality and source code security management

Principle, technology and implementation scheme of data consistency in distributed database

el-cascader回显失败;el-cascader回显不出来

Memtable for leveldb source code analysis

Practice of combining rook CEPH and rainbow, a cloud native storage solution

了解 JVM 中几个相关问题 — JVM 内存布局、类加载机制、垃圾回收

C# wpf 使用DockPanel实现截屏框

导电滑环使用的注意事项

Go learning notes (5) basic types and declarations (4)
随机推荐
RecycleView的一些使用
Series of improving enterprise product delivery efficiency (1) -- one click installation and upgrade of enterprise applications
Causes of short circuit of conductive slip ring and Countermeasures
How to traverse massive data in redis
Global and Chinese markets of InGaAs APD arrays 2022-2028: Research Report on technology, participants, trends, market size and share
More than one file was found with OS independent path ‘lib/armeabi-v7a/libyuv. so‘.
工业导电滑环的应用
Unity project experience summary
What can the points mall Games bring to businesses? How to build a points mall?
Detailed explanation of set
Leetcode522- longest special sequence ii- hash table - String - double pointer
Intelligent operation and maintenance: visual management system based on BIM Technology
More than one file was found with OS independent path ‘lib/armeabi-v7a/libyuv.so‘.
Global and Chinese market of 3D design and modeling software 2022-2028: Research Report on technology, participants, trends, market size and share
Some common commands of podman
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
Receiving package install and uninstall events
新手在挖财开通证券账户安全吗?
AcWing 888. Finding combinatorial number IV (the problem of finding combinatorial number with high precision)
Global and Chinese markets for soft ferrite cores 2022-2028: Research Report on technology, participants, trends, market size and share