当前位置:网站首页>Unity uses SQLite
Unity uses SQLite
2022-07-01 22:09:00 【Game programming】
I think the Internet is full of quotes sqlite3.dll,Mono.Data.Sqlite.dll,System.Data.dll, such as Setup Database (SQLite) for Unity
Or some third-party import methods unity-3rdparty-sqlite-net
Or import through a third-party package management tool Unity3D introduction : How to manage Unity In the project NuGet package ? Use of third parties NuGet Package manager ——NuGetForUnity
But I found that Unity About Sqlite The library of Unity.VisualScripting.Dependencies.Sqlite.SQLiteConnection
, Information has Unity.VisualScripting.Dependencies.Sqlite.SQLiteConnection
So I made a simple addition, deletion, modification and check demo,Unity.VisualScripting.Dependencies.Sqlite.SQLiteConnection Sure Use it directly
, It can be done. Yes Sqlite The operation of
Here's the key code
Create table
using (var db = new SQLiteConnection(databasePath, SQLiteOpenFlags.ReadWrite)){ var count = db.CreateTable<UserInfo>(); count = db.CreateTable<RandomEvent>();}
Add data
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));}
Modifying data
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));}
Delete data
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);}
Query data
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);}
Data sheets need to be declared
[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; }}
Sample code
TestSqliteScript
After opening the project , stay TestSqliteCanvas
Can be executed under
If there is something inappropriate , You can pack another layer by yourself ~
author :zLulus
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- Significance and measures of security encryption of industrial control equipment
- Talking from mlperf: how to lead the next wave of AI accelerator
- js数组拼接的四种方法[通俗易懂]
- Pytest collection (2) - pytest operation mode
- EMC-电路保护器件-防浪涌及冲击电流用
- 微软、哥伦比亚大学|GODEL:目标导向对话的大规模预训练
- 浏览器tab页之间的通信
- 【深度学习】利用深度学习监控女朋友的微信聊天?
- 编程英语生词笔记本
- Using closures to switch toggle by clicking a button
猜你喜欢
Flume面试题
latex如何打空格
Spark面试题
[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!
4. 对象映射 - Mapping.Mapstercover
pytest合集(2)— pytest運行方式
GCC编译
[noip2013] building block competition [noip2018] road laying greed / difference
ICML2022 | 基于元语义正则化的介入性对比学习
locust 系列入门
随机推荐
pytest合集(2)— pytest運行方式
latex如何打空格
MySQL empties table data
leetcode刷题:栈与队列04(删除字符串中的所有相邻重复项)
微软、哥伦比亚大学|GODEL:目标导向对话的大规模预训练
手动实现function isInstanceOf(child,Parent)
能升职加薪?PMP证书含金量浅析
Business visualization - make your flowchart'run'up
Significance and measures of security encryption of industrial control equipment
个人炒股怎样开户?安全吗。
Basic operation of binary tree
名单揭晓 | 2021年度中国杰出知识产权服务团队
Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan
UVM教程
vscode的使用
BC35&BC95 ONENET MQTT(旧)
4. 对象映射 - Mapping.Mapstercover
基于三维GIS的不动产管理应用
月入1W+的自媒体达人都会用到的运营工具
"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