You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity跨平台(Android/PC)SQLite查询异常:Android端提示“表未找到”

解决Android平台SQLite「表未找到」的问题

嘿,我之前也踩过类似的跨平台SQLite坑!核心问题其实是Android平台对StreamingAssets文件夹的访问限制——PC和编辑器里这个文件夹是直接可读写的,但Android上它是打包进APK的只读路径,SQLite根本没法正常从这里加载数据库,这就是你查不到表的根源。

下面给你一步步的解决方案:

1. 先把数据库复制到可读写路径

Android上唯一能稳定进行读写操作的路径是Application.persistentDataPath,所以启动游戏时得先检查这个路径下有没有数据库文件,没有的话就从StreamingAssets里复制过去。

给你一段可用的代码片段:

private string GetDatabasePath()
{
    #if UNITY_ANDROID && !UNITY_EDITOR
        string dbFileName = "你的数据库文件名.db";
        // 原StreamingAssets路径(Android上是jar包内的只读路径)
        string streamingDbPath = Path.Combine(Application.streamingAssetsPath, dbFileName);
        // 目标可读写路径
        string persistentDbPath = Path.Combine(Application.persistentDataPath, dbFileName);

        // 如果目标路径没有数据库,就从StreamingAssets复制
        if (!File.Exists(persistentDbPath))
        {
            // Android上不能直接用File.ReadAllBytes读StreamingAssets,得用UnityWebRequest
            using (UnityWebRequest dbRequest = UnityWebRequest.Get(streamingDbPath))
            {
                dbRequest.SendWebRequest();
                // 等待复制完成
                while (!dbRequest.isDone) { }

                if (dbRequest.result != UnityWebRequest.Result.Success)
                {
                    Debug.LogError("数据库复制失败:" + dbRequest.error);
                    return string.Empty;
                }

                // 把读取到的字节写入可读写路径
                File.WriteAllBytes(persistentDbPath, dbRequest.downloadHandler.data);
            }
        }
        return persistentDbPath;
    #else
        // PC和编辑器直接用StreamingAssets路径就行
        return Path.Combine(Application.streamingAssetsPath, "你的数据库文件名.db");
    #endif
}

2. 调整你的数据库连接逻辑

接下来,你所有的SQLite操作都要使用上面方法返回的路径,而不是直接用StreamingAssets的路径:

public void LoadDataIntoLists()
{
    string dbPath = GetDatabasePath();
    if (string.IsNullOrEmpty(dbPath))
    {
        Debug.LogError("获取数据库路径失败");
        return;
    }

    // 建立SQLite连接
    using (SqliteConnection dbConnection = new SqliteConnection($"URI=file:{dbPath}"))
    {
        dbConnection.Open();

        // 这里写你原来读取三个列表的查询逻辑
        // 比如:
        // using (SqliteCommand cmd = new SqliteCommand("SELECT * FROM 你的表名", dbConnection))
        // {
        //     using (SqliteDataReader reader = cmd.ExecuteReader())
        //     {
        //         while (reader.Read())
        //         {
        //             // 填充你的列表
        //         }
        //     }
        // }

        dbConnection.Close();
    }
}

3. 额外排查点

  • 确认数据库文件正确打包:在Unity里选中你的数据库文件,检查Inspector面板里的「Asset Label」是否包含StreamingAssets,确保它被正确打包进APK。
  • SQLite库兼容性:Android上的Mono.Data.Sqlite可能存在版本兼容问题,如果上面的方法还不行,可以试试更轻量化的跨平台SQLite库(比如SQLite-net),适配性会更好。
  • 表名拼写检查:虽然SQLite默认不区分大小写,但保险起见,确认你查询语句里的表名和数据库里的完全一致,没有拼写错误。

内容的提问来源于stack exchange,提问作者D. Shinoda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:56:18