【数据库】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("爱好"));
        }
    }
}
相关推荐
todoitbo5 小时前
CTE 与外层 JOIN 的条件下推:原理、边界与验证方法
数据库·join·cte
TEC_INO5 小时前
Linux56:读取人脸图片并把特征值保存到sqlite3数据库
数据库·oracle
刘~浪地球5 小时前
MongoDB分片集群实战:水平扩展海量数据
数据库·mongodb
鸽芷咕5 小时前
KingbaseES中的PL_SQL编程:存储过程、函数、触发器与包的开发指南
数据库·sql·oracle
4311媒体网5 小时前
帝国CMS新手入门教程:从零开始掌握企业级建站系统
数据库
韩小兔修媛史5 小时前
Redis面试八股文总结
数据库·redis·面试
小则又沐风a6 小时前
Linux下的Git的上传(版本控制器)
linux·数据库·git
赵渝强老师6 小时前
【赵渝强老师】PostgreSQL的数据预热扩展pg_prewarm
数据库·postgresql
小新同学^O^6 小时前
简单学习 --> 数据加密
java·数据库·学习·数据加密
Elastic 中国社区官方博客6 小时前
将 Logstash Pipeline 从 Azure Event Hubs 迁移到 OTel Collector Kafka Receiver
大数据·数据库·人工智能·分布式·elasticsearch·搜索引擎·kafka