当前位置:网站首页>Unity using Sqlite
Unity using Sqlite
2022-07-19 03:41:00 【zLulus】
I see online blogs talking about referencing sqlite3.dll,Mono.Data.Sqlite.dll,System.Data.dll,for example Setup Database (SQLite) for Unity
or using some of the third-party import methods unity-3rdparty-sqlite-net
or importing via third-party package management tools Unity3D introduction : How to manage Unity In the project NuGet package ? Use of third parties NuGet Package manager ——NuGetForUnity.
But I found out that Unity has libraries for Sqlite. It is Unity.VisualScripting.Dependencies.Sqlite.SQLiteConnection
,The information is here:Unity.VisualScripting.Dependencies.Sqlite.SQLiteConnection
So I simply made a demo of inserting, deleting, and querying.
Unity.VisualScripting.Dependencies.Sqlite.SQLiteConnection can use directly without any references
, so you can complete the operation of Sqlite.
Here is part of the key code.
Create Table
using (var db = new SQLiteConnection(databasePath, SQLiteOpenFlags.ReadWrite))
{
var count = db.CreateTable<UserInfo>();
count = db.CreateTable<RandomEvent>();
}
Insert 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));
}
Modify 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);
}
The data table needs 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, you can execute it in the scene TestSqliteCanvas
.
If you don’t feel comfortable using it, you can wrap another layer yourself~
边栏推荐
- Unity solves the problem of Z-fighting caused by overlapping objects with the same material
- ES6學習筆記——B站小馬哥
- Latest installation tutorial of VMware Tools (rhel8)
- Solve the error of 0x00000709 when win10 connects to the shared printer
- GoogLeNet
- Shell script receives and returns parameters
- Analysis and processing of male and female voice signals based on MATLAB
- [MySQL] data query operation (select statement)
- 支持工业级瘦设备4G接入,润和软件DAYU120通过OpenHarmony兼容性测评
- 基于Matlab的男女声音信号分析与处理
猜你喜欢
Browser cannot open tensorboard
Work fishing clock simulator wechat applet source code
web语义化(强调标签-em-斜体)(重点强调标签-strong-粗体)(自定义列表:dl、dt、dd)
GNOME-BOXES虚拟机创建安装
Installing PWA application in Google Chrome browser will display more description information
GoogLeNet
STM32串口发送和接收多个数据教程基于气体传感器实战
Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]
374. Guess the size of numbers (must be able to get started)
Vscode+ros2 environment configuration
随机推荐
Leetcode: 0-1 knapsack problem in dynamic programming [come and set the template directly]
[NoSQL] redis configuration and optimization of NoSQL (simple operation)
Rhce8 Learning Guide Chapter 1 installing rhel8.4
Transaction and storage engine in MySQL database
基于Matlab的男女声音信号分析与处理
ES6 learning notes - brother Ma at station B
Unity solves the problem of Z-fighting caused by overlapping objects with the same material
Installing PWA application in Google Chrome browser will display more description information
MySQL addition, deletion, query and modification (basic)
367. 有效的完全平方数(入门必会)
Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis
Chapter I Introduction
Edge detection method -- first order edge detection
leetcode:50. Pow(x, n)
Leetcode: multiple knapsack problem in dynamic programming [one template solves all ~]
Machine learning library scikit learn (linear model, ridge regression, insert a column of data, extract the required column, vector machine (SVM), clustering)
論文閱讀:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
论文阅读:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
[MySQL] MHA high availability
Rhce8 Study Guide Chapter 2 use of basic commands