【数据库】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("爱好"));
        }
    }
}
相关推荐
沉到海底去吧Go16 分钟前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名
老纪的技术唠嗑局33 分钟前
重剑无锋,大巧不工 —— OceanBase 中的 Nest Loop Join 使用技巧分享
数据库·sql
未来之窗软件服务1 小时前
JAVASCRIPT 前端数据库-V6--仙盟数据库架构-—-—仙盟创梦IDE
数据库·数据库架构·仙盟创梦ide·东方仙盟·东方仙盟数据库
一只爱撸猫的程序猿2 小时前
构建一个简单的智能文档问答系统实例
数据库·spring boot·aigc
nanzhuhe3 小时前
sql中group by使用场景
数据库·sql·数据挖掘
消失在人海中3 小时前
oracle sql 语句 优化方法
数据库·sql·oracle
Clang's Blog3 小时前
一键搭建 WordPress + MySQL + phpMyAdmin 环境(支持 PHP 版本选择 & 自定义配置)
数据库·mysql·php·wordpr
zzc9213 小时前
MATLAB仿真生成无线通信网络拓扑推理数据集
开发语言·网络·数据库·人工智能·python·深度学习·matlab
未来之窗软件服务3 小时前
JAVASCRIPT 前端数据库-V1--仙盟数据库架构-—-—仙盟创梦IDE
数据库·数据库架构·仙盟创梦ide·东方仙盟数据库
LjQ20403 小时前
网络爬虫一课一得
开发语言·数据库·python·网络爬虫