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

打印 上一主题 下一主题

主题 1964|帖子 1964|积分 5892

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.找到必要三个 DLL



  • Mono.Data.Sqlite.dll
  • System.Data.dll
  • sqlite3.dll
上面两个dll可在本地unity安装目次找到:
  1. C:\Program Files\Unity\Hub\Editor\2022.3.xxf1c1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit-win32
复制代码
下面dll可在sqlite官网下载到:
  1. https://www.sqlite.org/download.html
复制代码

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


3.测试代码:

  1. using UnityEngine;
  2. using Mono.Data.Sqlite;
  3. using System.Data;
  4. public class SQLiteTest : MonoBehaviour
  5. {
  6.     private string dbPath;
  7.     void Start()
  8.     {
  9.         // SQLite 数据库的路径
  10.         dbPath = "URI=file:" + Application.streamingAssetsPath + "/example.db";
  11.         Debug.Log("Database Path: " + dbPath);
  12.         CreateTable();
  13.         InsertData("TestKey", "TestValue");
  14.         string value = GetData("TestKey");
  15.         Debug.Log("Retrieved Value: " + value);
  16.     }
  17.     private void CreateTable()
  18.     {
  19.         using (IDbConnection dbConnection = new SqliteConnection(dbPath))
  20.         {
  21.             dbConnection.Open();
  22.             using (IDbCommand command = dbConnection.CreateCommand())
  23.             {
  24.                 command.CommandText = "CREATE TABLE IF NOT EXISTS MyTable (key TEXT PRIMARY KEY, value TEXT)";
  25.                 command.ExecuteNonQuery();
  26.             }
  27.         }
  28.     }
  29.     private void InsertData(string key, string value)
  30.     {
  31.         using (IDbConnection dbConnection = new SqliteConnection(dbPath))
  32.         {
  33.             dbConnection.Open();
  34.             using (IDbCommand command = dbConnection.CreateCommand())
  35.             {
  36.                 command.CommandText = "INSERT OR REPLACE INTO MyTable (key, value) VALUES (@key, @value)";
  37.                 command.Parameters.Add(new SqliteParameter("@key", key));
  38.                 command.Parameters.Add(new SqliteParameter("@value", value));
  39.                 command.ExecuteNonQuery();
  40.             }
  41.         }
  42.     }
  43.     private string GetData(string key)
  44.     {
  45.         using (IDbConnection dbConnection = new SqliteConnection(dbPath))
  46.         {
  47.             dbConnection.Open();
  48.             using (IDbCommand command = dbConnection.CreateCommand())
  49.             {
  50.                 command.CommandText = "SELECT value FROM MyTable WHERE key = @key";
  51.                 command.Parameters.Add(new SqliteParameter("@key", key));
  52.                 using (IDataReader reader = command.ExecuteReader())
  53.                 {
  54.                     if (reader.Read())
  55.                     {
  56.                         return reader.GetString(0);
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.         return null;
  62.     }
  63.     private void Update()
  64.     {
  65.         if (Input.GetKeyDown(KeyCode.T))
  66.         {
  67.             Debug.Log("写入数据");
  68.             InsertData("名字", "蜡笔小新");
  69.             InsertData("年龄", "5岁");
  70.             InsertData("职业", "幼儿园学生");
  71.             InsertData("爱好", "大姐姐");
  72.         }
  73.         if (Input.GetKeyDown(KeyCode.Space))
  74.         {
  75.             Debug.Log(GetData("名字"));
  76.             Debug.Log(GetData("年龄"));
  77.             Debug.Log(GetData("职业"));
  78.             Debug.Log(GetData("爱好"));
  79.         }
  80.     }
  81. }
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

笑看天下无敌手

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表