当前位置:网站首页>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~
边栏推荐
- MySQL master-slave setup
- Analysis and processing of male and female voice signals based on MATLAB
- options has an unknown property ‘before‘
- 通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
- Thinkphp5.0 model operation uses page for paging
- leetcode:50. Pow(x, n)
- 机器学习库Scikit-Learn(线性模型、岭回归、插入一列数据(insert)、提取所需列、向量机(SVM)、聚类)
- MySQL log management and full backup incremental backup and recovery
- 367. Effective complete square (necessary for entry)
- Leetcode: subsequence problem in dynamic programming
猜你喜欢

IEEE754 standard floating point format

Rhce8 Learning Guide Chapter 1 installing rhel8.4

Bias and variance

Envi: (the most detailed tutorial in 2022) custom coordinate system
![mysqldump: [Warning] Using a password on the command line interface can be insecure.](/img/91/8b0d35f85bc0f46daac4e1e9bc9e34.png)
mysqldump: [Warning] Using a password on the command line interface can be insecure.

Wdog and power mode of fs32k148 commissioning

367. 有效的完全平方数(入门必会)

HRNet

Zabbix6.0 monitors Dell and IBM server hardware through Idrac and imm2

Laravel's problem
随机推荐
Transaction and storage engine in MySQL database
Chengxin University envi_ IDL second week class content: open hdf4 file and read the file, as well as simple data processing and saving + detailed analysis
第一章 绪论
[MySQL] data query operation (select statement)
ES6学习笔记——B站小马哥
central limit theorem
如何将Excel中的数据粘贴到cxGrid中
mysql创建项目研发账号
Browser cannot open tensorboard
374. 猜数字大小(入门 必会)
Receiver operating curve
Zabbix6.0 monitoring vcenter7.0
Net SNMP related commands
爬虫学习(5):手把手教你爬虫requests实战演练
oracle 查询 主机名和对应的IP地址
Vs code problem: launch:program '... \ vscode\launch. exe‘ dose not exist
Frequency school and Bayes school
sublime基本操作
How to use iota keyword in go language
Win10 onedrive failure reinstallation