【数据库】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("爱好"));
        }
    }
}
相关推荐
龙仔72510 分钟前
SQL Server 批量多库只读账号一键脚本(带逐行注释完整脚本 + 分段逐句详细解释)
服务器·数据库·sql·sqlserver·dba
咏方舟【长江支流】1 小时前
【开源】跨语言·跨平台·跨数据库(2) —— 用宝轻量框架设计必读
数据库·架构·开源·ai编程·咏方舟-长江支流·用宝框架
手握风云-1 小时前
Redis:不只是缓存那么简单(十五)
数据库·redis·缓存
weixin_307779132 小时前
Linux下Docker Compose里运行PostgreSQL数据库故障诊断Shell脚本
linux·运维·数据库·docker·postgresql
醉城夜风~2 小时前
MySQL 常用内置函数全解:字符串、数值、日期、流程控制实战教程
数据库·mysql
程序猿秃头之路2 小时前
DDD 系列:实体和值对象详解
数据库·oracle·ddd·领域驱动设计
Database_Cool_2 小时前
阿里云 RDS MySQL vs 腾讯云 CDB 国内云关系型数据库深度对比
数据库·mysql·阿里云
xiaohaiAIgeo2 小时前
【2026年】基于三维建模的实验室智慧管理平台:暖通能源照明的数字孪生方案
大数据·数据库·人工智能·科普知识
深盾科技_Virbox3 小时前
软件授权工具静默安装实践
java·运维·数据库·安全·软件需求
延凡科技4 小时前
延凡科技综合监控预警处置平台—— 一体化视频AI安防闭环系统设计与功能实现[特殊字符]️
数据库·人工智能·科技·安全·能源