【数据库】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("爱好"));
        }
    }
}
相关推荐
bing_1581 小时前
如何利用 Redis 的原子操作(INCR, DECR)实现分布式计数器?
数据库·redis·分布式
一个人的博客@你2 小时前
C#多数据库批量执行脚本工具
数据库·c#·批量执行·sql脚本·多数据库执行
果冻kk2 小时前
MySQL MVCC:并发神器背后的原理解析
数据库·mysql
卍郝凝卍2 小时前
云上服务器常见的存储方式和类型
大数据·服务器·数据库
axban3 小时前
QT中删除控件的注意事项、deleteLater和delete的区别
java·数据库·qt
♪张三儿℡4 小时前
Oracle优化学习十六
数据库·学习·oracle
胡琦博客4 小时前
LLM Prompt与开源模型资源(3)如何写一个好的 Prompt
数据库·开源·prompt
betazhou4 小时前
postgresql创建只读用户并授权
数据库·postgresql
Fireworkitte4 小时前
SQL 中 CASE WHEN 及 SELECT CASE WHEN 的用法
数据库·sql·mysql
小云数据库服务专线5 小时前
GaussDB 数据库设计规范
数据库·设计规范·gaussdb