【问题标题】:no such table: Users没有这样的表:用户
【发布时间】:2021-08-05 21:28:57
【问题描述】:

我在我的游戏中添加了一个数据库,当我在 Unity Editor 中签入时一切正常,但是当我在手机上运行游戏时出现错误 no such table: Users 虽然数据库有这个表

我的代码

    private void ConnectData()
    {
#if UNITY_EDITOR
        _path = Path.Combine(Application.streamingAssetsPath, "russianWords.bytes"); //Path to datab
#elif PLATFORM_ANDROID
        _path = Application.persistentDataPath + "russianWords.bytes";
        if (!File.Exists(_path))
        {
                Debug.Log("4");
            string timlesPath = "jar:file://" + Application.dataPath + "!/assets" + "russianWords.bytes";
            Debug.Log(timlesPath);
            WWW load = new WWW(timlesPath);
            while (!load.isDone) { }
            File.WriteAllBytes(_path, load.bytes);
        }

#endif

        _dbConnection = new SqliteConnection("URI=file:" + _path);
        _dbConnection.Open();
        if (_dbConnection.State == ConnectionState.Open)
        {
            m_sqlCmd = new SqliteCommand();
            m_sqlCmd.Connection = _dbConnection;
        }
    }

private void CheckEndGame()
{
    if(ResultWords.Count - 1 >= GameManager.Instance.TargetWordsAmount)
    {
        string words = "";
        foreach(var p in ResultWords)
        {
            if(p != "КОЛА")
                words += p + ", ";
        }

        ScrambleInterface.Interface.OpenResultPanel(words);
    }
}

【问题讨论】:

  • 我看不到您访问该表的位置。 while (!load.isDone) { } 应该做什么?
  • 这个循环将一直运行,直到表格被加载。
  • 是的,但load.isDone 不会在循环内部发生变化,是吗?
  • 是的,如果循环改变,那么代码会更进一步
  • @Charlieface 过时 WWW 异步运行,因此isDone 将在单独的Task 上变为true

标签: c# android sqlite unity3d


【解决方案1】:

在我看来,您的 WWW (which is long obsolete btw) 只是失败了,因为您在错误的位置查找文件。

您没有任何成功或失败的检查,所以我只想声称:加载失败,因此load.bytes 是一个空的byte[]。 -> 该数据库中根本不存在任何内容。

你也是,例如使用

_path = Application.persistentDataPath + "russianWords.bytes";

这是您的应用甚至无法访问的路径 -> 您可能已经收到了 File.Exists 的异常。


如前所述,WWW 已过时。你也不应该对这种东西使用阻塞调用。我什至根本不会在这里使用 WebRequests,而只是直接使用FileIO,例如

    private void Connect()
    {
        Task.Run(async () => await ConnectAsync());
    }

    private async Task ConnectAsync()
    {
#if UNITY_EDITOR
        _path = Path.Combine(Application.streamingAssetsPath, "russianWords.bytes");
#elif PLATFORM_ANDROID
        _path = Path.Combine(Application.persistentDataPath, "russianWords.bytes");
        if (!File.Exists(_path))
        {
            Debug.Log("4");
            var sourcePath = Path.Combine(Application.streamingAssetsPath, "russianWords.bytes");
            Debug.Log(sourcePath);
            
            await CopyFileAsync(sourcePath, _path);
        }
#endif

        _dbConnection = new SqliteConnection("URI=file:" + _path);
        _dbConnection.Open();
        if (_dbConnection.State == ConnectionState.Open)
        {
            m_sqlCmd = new SqliteCommand();
            m_sqlCmd.Connection = _dbConnection;
        }
    }

    private async Task CopyFileAsync(string sourcePath, string destinationPath)
    {
        using (var source = File.Open(sourcePath))
        {
            using(var destination = File.Create(destinationPath))
            {
                await source.CopyToAsync(destination);
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2021-01-21
    • 2020-03-01
    • 2021-11-02
    • 2020-05-15
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2022-10-06
    • 2011-10-24
    相关资源
    最近更新 更多