Unity3D连接本地SQLite进行数据的增删查改

早就在琢磨Unity3D连接SQLite这个东西,网上的相关资料很少,而且不完整,各种坑需要填。工作之余,抽出一点时间写下这篇博客,让其他童鞋少走一点弯路。

首先,就是准备相关的DLL文件,这些是必不可少的,笔者已经为你准备好了,很多其他博客上的那些DLL文件都是有问题或者是缺失的。

  https://pan.baidu.com/s/1TiUDy7Dz5tkFwT04YWbaAQ     提取码:89ft

plugins下是相关DLL和雨松写的SQLite辅助脚本,但是他的脚本存在一个坑,插入字符串的时候会报错,因为他脚本里少写了个单引号(被坑过),这里笔者已经做了修改, streamingAssets下是一个Sqlite文件,这些文件必须在这些指定的文件夹下,否则会出各种各样的问题(具体什么问题懒得描叙了,自己去试试,反正笔者是被坑了)。Test脚本中写了一些增删改查的示例。

然后就是连接的问题了,要注意,Unity3D连接SQLite在各平台下的路径是不同的,笔者自己只在Android和pc,编辑器下使用过,其他平台请参考      https://habr.com/post/181239/

下面是笔者很久以前写的,刚学Unity3D写单机游戏的时候写过的一个简陋的数据管理脚本,仅供参考。特别要注意的是,在安卓手机上每一次存取数据,都要重新拷贝一次,否则数据无法保存,所以笔者将他写在了path属性的get方法中。

​using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;
using System.IO;

public class DataManager : MonoBehaviour {

    public static DataManager instance;

    /// <summary>
    /// 路径
    /// </summary>
    private string path
    {
        get
        {
            string appDBPath = "";
#if UNITY_EDITOR //编辑器下
            appDBPath = Application.streamingAssetsPath + "/HanYu.db";//通过路径找到第三方数据库
#elif UNITY_ANDROID//如果运行在Android设备中
        appDBPath =Application.persistentDataPath  + "/HanYu.db";//(手机的沙盒目录)
        if (!File.Exists(appDBPath))
        {
            WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "HanYu.db");
            while (!loadDB.isDone) { }//等待下载完成
            File.WriteAllBytes(appDBPath, loadDB.bytes);//拷贝至规定的地方
        }
#elif UNITY_STANDALONE_WIN //PC
         appDBPath= Application.streamingAssetsPath + "/HanYu.db";    
#endif
            return "URI=file:" + appDBPath;
        }
    }
	// Use this for initialization
	void Awake ()
    {
        if (instance == null) instance = this;
    }

    /// <summary>
    /// 获取数据--0.等级 1.经验  2.金币
    /// </summary>
    /// <returns></returns>
    public int[] Read()
    {
        Debug.Log("读数据");
        SQLite DB = new SQLite(path);//打开数据库
        SqliteDataReader data = Reader(DB, "*");
        data.Read();//读取数据NO.3
        int[] array = new int[3];
        for (int i = 0; i < 3; i++)//数据库列数-2
            array[i] = data.GetInt32(i+2);
        DB.CloseSqlConnection();
        return array;
    }

    /// <summary>
    /// 获取玩家昵称
    /// </summary>
    /// <returns></returns>
    public string GetName()
    {
        SQLite DB = new SQLite(path);//打开数据库
        SqliteDataReader data = Reader(DB, "name");
        data.Read();//读取数据NO.3
        string str = data.GetString(0);
        DB.CloseSqlConnection();
        return str;
    }

    /// <summary>
    /// 设置玩家昵称
    /// </summary>
    /// <param name="value"></param>
    public void SetName(string value)
    {
        SQLite DB = new SQLite(path);//打开数据库
        DB.UpdateInto("User", new string[] { "name" }, new string[] { value }, "id", "2008");
        DB.CloseSqlConnection();
    }

    /// <summary>
    /// 修改数据--0.等级 1.经验  2.金币
    /// </summary>
    /// <param name="column"></param>
    /// <param name="value"></param>
    public void Write(int column,int value)
    {
        Debug.Log("写数据");
        SQLite DB = new SQLite(path);//打开数据库
        string cols = "";
        switch (column+2)
        {
            case 2:cols = "level";break;
            case 3:cols = "exp";break;
            case 4:cols = "coin";break;
            default:Debug.LogError("越界");break;
        }
        DB.UpdateInto("User", new string[] { cols }, new string[] { value.ToString() }, "id", "2008");
        DB.CloseSqlConnection();
    }

    private static SqliteDataReader Reader(SQLite DB,string key)
    {
        SqliteDataReader data = DB.SelectWhere("User", new string[] { key }, new string[] { "id" }, new string[] { "=" }, new string[] { "2008" });
        return data;
    }

}
​

另外一点要注意的是,每次使用数据库之后都要记得关闭,否则可能会导致数据库锁死。

 

猜你喜欢

转载自blog.csdn.net/qq_39824797/article/details/83304916
今日推荐