当前位置:网站首页>Unity 使用Sqlite
Unity 使用Sqlite
2022-07-01 21:39:00 【游戏编程】
我看网上都在讲引用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 下即可执行
如果觉得有不合适的地方,可以自己再封装一层~
作者:zLulus
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- News classification based on LSTM model
- 杰理之、产线装配环节【篇】
- 2022熔化焊接与热切割上岗证题目模拟考试平台操作
- Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc
- cmake工程化相关
- 【Opencv450】HOG+SVM 与Hog+cascade进行行人检测
- Aidl basic use
- 合成大西瓜小游戏微信小程序源码/微信游戏小程序源码
- 2022安全员-B证考试练习题模拟考试平台操作
- ICML2022 | 基于元语义正则化的介入性对比学习
猜你喜欢

JS how to get a list of elements in a collection object

【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程

Flume面试题

2022熔化焊接与热切割上岗证题目模拟考试平台操作

【Opencv450】HOG+SVM 与Hog+cascade进行行人检测

手动实现function isInstanceOf(child,Parent)

Talking from mlperf: how to lead the next wave of AI accelerator
![[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!](/img/27/4bd0de716f5cb360d54f140dc8e9c1.png)
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!

Halcon知识:三维重构的一个尝试

芭比Q了!新上架的游戏APP,咋分析?
随机推荐
locust 系列入门
BlocProvider 为什么感觉和 Provider 很相似?
Application of real estate management based on 3D GIS
MIT|256KB 内存下的设备上训练
C中main函数的几种写法
Can I choose to open an account for stock trading on flush? Is it safe?
函数基本学习之一
杰理之烧录上层版物料需要【篇】
php反射型xss,反射型XSS测试及修复
辅音和声母的区别?(声母与辅音的区别)
中通笔试题:翻转字符串,例如abcd打印出dcba
杰理之蓝牙耳机品控和生产技巧【篇】
【opencv】train&test HOG+SVM
"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition
Pytest collection (2) - pytest operation mode
PWN攻防世界cgpwn2
pytest合集(2)— pytest運行方式
薛定谔的日语学习小程序源码
Test cancellation 1
Why does blocprovider feel similar to provider?