当前位置:网站首页>Unity using Sqlite
Unity using Sqlite
2022-07-17 01:33: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 入门:如何管理 Unity 项目中的 NuGet 包?使用第三方 NuGet 包管理器——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~
边栏推荐
- Use RZ, SZ commands to upload and download files through xshell7
- 谷歌 Chrome 浏览器安装 PWA 应用将显示更多描述信息
- Number of supported question banks and examination question banks of swiftui examination question bank project (tutorial includes source code)
- MySQL interview questions (2022)
- KubeCon + CloudNativeCon Europe 2022
- Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis
- 2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
- Le cinquième jour de trois questions par jour à luogu
- Monte Carlo based reinforcement learning method [with code implementation]
- oracle 查询非自增长分区的最大分区
猜你喜欢

MySQL addition, deletion, query and modification (basic)

谷歌 Chrome 浏览器安装 PWA 应用将显示更多描述信息

About 1000base-t1 1000Base-TX and 100base-t1

laravel的问题

How to read and write a single document based on MFC
![Theoretical basis of doubledqn and its code implementation [pytoch + pendulum-v0]](/img/f2/4a3cc8e5173789111080915aceb9fd.png)
Theoretical basis of doubledqn and its code implementation [pytoch + pendulum-v0]

Zabbix6.0 monitors Dell and IBM server hardware through Idrac and imm2
![Leetcode: dynamic programming [basic problem solving]](/img/fc/0ff622576a47868f8ecf70116ea2f2.png)
Leetcode: dynamic programming [basic problem solving]

ES6 learning notes - brother Ma at station B

Zabbix6.0 monitoring vcenter7.0
随机推荐
Yolov5 ncnn reasoning
Rewrite equals why rewrite hashcode
谷歌 Chrome 浏览器安装 PWA 应用将显示更多描述信息
leetcode162. Looking for peak
论文阅读:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
Vscode+ros2 environment configuration
Leetcode: subsequence problem in dynamic programming
Digital type processing and convenient method of ext JS
XX City high school network topology overall planning configuration
Common MySQL commands you can use
Bisenetv2 face segmentation ncnn reasoning
Authentication code for wireless
Laravel's problem
About 1000base-t1 1000Base-TX and 100base-t1
Unity解决同材质物体重叠产生Z-Fighting的问题
ES6 learning notes - brother Ma at station B
Envi: (the most detailed tutorial in 2022) custom coordinate system
Transaction and storage engine in MySQL database
oracle 查询 主机名和对应的IP地址
Gnome boxes virtual machine creation and installation