【数据库】Unity 使用 Sqlite 数据库

1.找到需要三个 DLL

  • Mono.Data.Sqlite.dll
  • System.Data.dll
  • sqlite3.dll

上面两个dll可在本地unity安装目录找到:

C:\Program Files\Unity\Hub\Editor\2022.3.xxf1c1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit-win32

下面dll可在sqlite官网下载到:

https://www.sqlite.org/download.html

2.将以上三个dll放入unity的里面的Plugins里面

3.测试代码:

cs 复制代码
using UnityEngine;
using Mono.Data.Sqlite;
using System.Data;

public class SQLiteTest : MonoBehaviour
{
    private string dbPath;

    void Start()
    {
        // SQLite 数据库的路径
        dbPath = "URI=file:" + Application.streamingAssetsPath + "/example.db";
        Debug.Log("Database Path: " + dbPath);

        CreateTable();
        InsertData("TestKey", "TestValue");
        string value = GetData("TestKey");
        Debug.Log("Retrieved Value: " + value);
    }

    private void CreateTable()
    {
        using (IDbConnection dbConnection = new SqliteConnection(dbPath))
        {
            dbConnection.Open();
            using (IDbCommand command = dbConnection.CreateCommand())
            {
                command.CommandText = "CREATE TABLE IF NOT EXISTS MyTable (key TEXT PRIMARY KEY, value TEXT)";
                command.ExecuteNonQuery();
            }
        }
    }

    private void InsertData(string key, string value)
    {
        using (IDbConnection dbConnection = new SqliteConnection(dbPath))
        {
            dbConnection.Open();
            using (IDbCommand command = dbConnection.CreateCommand())
            {
                command.CommandText = "INSERT OR REPLACE INTO MyTable (key, value) VALUES (@key, @value)";
                command.Parameters.Add(new SqliteParameter("@key", key));
                command.Parameters.Add(new SqliteParameter("@value", value));
                command.ExecuteNonQuery();
            }
        }
    }

    private string GetData(string key)
    {
        using (IDbConnection dbConnection = new SqliteConnection(dbPath))
        {
            dbConnection.Open();
            using (IDbCommand command = dbConnection.CreateCommand())
            {
                command.CommandText = "SELECT value FROM MyTable WHERE key = @key";
                command.Parameters.Add(new SqliteParameter("@key", key));
                using (IDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return reader.GetString(0);
                    }
                }
            }
        }
        return null;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            Debug.Log("写入数据");
            InsertData("名字", "蜡笔小新");
            InsertData("年龄", "5岁");
            InsertData("职业", "幼儿园学生");
            InsertData("爱好", "大姐姐");
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log(GetData("名字"));
            Debug.Log(GetData("年龄"));
            Debug.Log(GetData("职业"));
            Debug.Log(GetData("爱好"));
        }
    }
}
相关推荐
羱滒12 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
程序员Android14 分钟前
MTK多帧拍照流程分析
数据库
不剪发的Tony老师16 分钟前
Apache Doris:一款高性能的实时数据仓库
数据库·数据仓库
甜可儿27 分钟前
redis序列化设置
数据库·redis
GGBondlctrl32 分钟前
【SpringBoot】论坛项目中如何进行实现发布文章,以及更新对应数据库的数据更新
数据库·springboot项目·三层设计思想·文章发布项目
若云止水1 小时前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
jay丿1 小时前
Django模板系统深入
数据库·django·sqlite
千层冷面2 小时前
Redis除了做缓存还能做什么?
数据库·redis·缓存
码有余悸3 小时前
Redis:高性能的键值存储系统
数据库·redis·缓存
剑客狼心7 小时前
Oracle:什么是存储过程
数据库·oracle