当前位置:网站首页>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 .
边栏推荐
猜你喜欢

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

杰理之、产线装配环节【篇】

List announced | outstanding intellectual property service team in China in 2021

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

ngnix基础知识

Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue

leetcode刷题:栈与队列06(前 K 个高频元素)

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

leetcode刷题:栈与队列01(用栈实现队列)

Flume面试题
随机推荐
php反射型xss,反射型XSS测试及修复
Application of real estate management based on 3D GIS
idea中类中显示成员变量和方法
MySQL series transaction log redo log learning notes
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
能升职加薪?PMP证书含金量浅析
杰理之关于长按开机检测抬起问题【篇】
leetcode刷题:栈与队列06(前 K 个高频元素)
基于YOLOv5的口罩佩戴检测方法
【级联分类器训练参数】Training Haar Cascades
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
杰理之蓝牙耳机品控和生产技巧【篇】
Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
Can I choose to open an account for stock trading on flush? Is it safe?
pytest合集(2)— pytest运行方式
Simple interactive operation of electron learning (III)
Talking from mlperf: how to lead the next wave of AI accelerator
杰理之、产线装配环节【篇】
开环和闭环是什么意思?
Aidl basic use