菜鸟教程小白 发表于 2022-12-11 18:59:42

c# - unity - IOS persistentDataPath access denied//无法打开数据库


                                            <p><p>我正在尝试从我的 streamingAssets 中恢复一个文件并在 persistentDataPath 中复制它。</p>

<p>问题是iOS拒绝访问persistentDataPath,所以我无法写入文件,谁能告诉我为什么?</p>

<p>代码:</p>

<pre><code>#elif UNITY_IOS
if (File.Exists(Application.dataPath + &#34;/Raw/&#34; + StaticDatas.databaseName))
{
    byte[] bytes = null;
       if (File.Exists(Application.dataPath + &#34;/Raw/&#34; + StaticDatas.databaseName))
       {
      Debug.Log(Application.dataPath + &#34;/Raw/&#34; + StaticDatas.databaseName);
      Debug.Log(StaticDatas.databasePath + StaticDatas.databaseName);
         using (FileStream fileStream = File.OpenRead(Application.dataPath + &#34;/Raw/&#34; + StaticDatas.databaseName))
         {
               bytes = new byte;
               fileStream.Read(bytes,0,int.Parse(fileStream.Length+&#34;&#34;));
               fileStream.Close();
               fileStream.Dispose();
         }
         FileStream fs = File.Create(StaticDatas.databasePath + StaticDatas.databaseName);
         fs.Write(bytes, 0, bytes.Length);
       }
         _connection = new SQLiteConnection(filePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
      CreateDB();
}
</code></pre>

<p>错误:
访问路径/var/mobile/containers/dta/application/manythings/DocumentsDatabaseName.db 被拒绝</p>

<p>IOS:9.5.3
统一:5.5.0f3</p>

<p><strong>------------------------------------------ --------------------</strong></p>

<p>更新:StaticDatas.databasePath = persistentDataPath</p>

<p><strong>------------------------------------------ --------------------</strong></p>

<p>更新 2:</p>

<p>好吧,我完全迷路了......</p>

<p>这是我的代码:</p>

<pre><code>#elif UNITY_IOS

string basePath = Path.Combine(Application.dataPath + &#34;Raw&#34; , StaticDatas.databaseName);
string targetPath = Path.Combine(StaticDatas.databasePath , StaticDatas.databaseName );
Debug.Log(basePath);
Debug.Log(targetPath);
if (File.Exists(basePath))
{
    byte[] bytes = null;

      Debug.Log(&#34;base path exists&#34;);
         using (FileStream fileStream = File.OpenRead(basePath))
         {
               Debug.Log(&#34;create byte array&#34;);
               bytes = new byte;
               Debug.Log(&#34; READ BYTES&#34;);
               fileStream.Read(bytes,0,int.Parse(fileStream.Length+&#34;&#34;));
               Debug.Log(&#34; CLOSE&#34;);
               fileStream.Close();
               Debug.Log(&#34;DISPOSE&#34;);
               fileStream.Dispose();
         }
         Debug.Log(&#34; Check if dir exists&#34;);
          /* if (!Directory.Exists(StaticDatas.databasePath + &#34;/AnnotationTest.app/database/&#34;))
         {
                Debug.Log(&#34; create folder&#34;);
                Directory.CreateDirectory(StaticDatas.databasePath + &#34;/AnnotationTest.app/database/&#34;);
         }*/
         Debug.Log(&#34;Open file&#34;);
         FileStream fs = File.Open(targetPath, FileMode.OpenOrCreate);
         Debug.Log(&#34;Write file&#34;);
         fs.Write(bytes, 0, bytes.Length);
         Debug.Log(&#34; CLOSE STRREAM&#34;);
         fs.Close();
       Debug.Log(&#34;Connec close&#34;);
         _connection = new SQLiteConnection(targetPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
         Debug.Log(&#34;sql connect&#34;);
      CreateDB();
      Debug.Log(&#34; db made&#34;);
}



if (File.Exists(&#34;file:&#34; + Application.dataPath + &#34;/Raw/&#34; + StaticDatas.databaseName));
{
    Debug.Log(&#34;file datapath raw databasename&#34;);
          byte[] bytes = null;

      Debug.Log(&#34;base path exists&#34;);
         using (FileStream fileStream = File.OpenRead(&#34;file:&#34; + Application.dataPath + &#34;/Raw/&#34; + StaticDatas.databaseName))
         {
               Debug.Log(&#34;create byte array&#34;);
               bytes = new byte;
               Debug.Log(&#34; READ BYTES&#34;);
               fileStream.Read(bytes,0,int.Parse(fileStream.Length+&#34;&#34;));
               Debug.Log(&#34; CLOSE&#34;);
               fileStream.Close();
               Debug.Log(&#34;DISPOSE&#34;);
               fileStream.Dispose();
         }
            FileStream fs = File.Open(targetPath, FileMode.OpenOrCreate);
         Debug.Log(&#34;Write file&#34;);
         fs.Write(bytes, 0, bytes.Length);
         Debug.Log(&#34; CLOSE STRREAM&#34;);
         fs.Close();

         _connection = new SQLiteConnection(targetPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
      CreateDB();
}

#else
            var loadDb = StaticDatas.databasePath + StaticDatas.databaseName;
            File.Copy(loadDb, filePath);
         _connection = new SQLiteConnection(filePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
      CreateDB();
#endif
</code></pre>

<p>第一个 if 语句确实工作了一会儿(没有 <code>combine</code>),但现在,花生。</p>

<p>第二个返回true(<code>Exists</code>),但是当它到达using语句时,它说“找不到路径的一部分”(即使它在if语句中找到它, wtf?)</p>

<p><strong>------------------------------------------ --------------------</strong></p>

<p>更新 3</p>

<p>这是一个奇迹!我找到了!
嗯,现在,SQLite 无法打开数据库,但我找到了!</p>

<pre><code>string bpa = Application.dataPath + &#34;/Raw/&#34; + StaticDatas.databaseName;

if (File.Exists(bpa))
{
    byte[] b = null;
    using (FileStream fs = File.OpenRead(bpa))
    {
      b = new byte;
      fs.Read(b, 0, int.Parse(fs.Length+&#34;&#34;));
      fs.Close();
      fs.Dispose();
    }
    FileStream fsa = File.Open(targetPath, FileMode.OpenOrCreate);
    fsa.Write(b,0, b.Length);
    fsa.Close();
    _connection = new SQLiteConnection(targetPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
      CreateDB();
}
else if (File.Exists(basePath))
{

byte[] b = null;
    using (FileStream fs = File.OpenRead(basePath))
    {
      b = new byte;
      fs.Read(b, 0, int.Parse(fs.Length+&#34;&#34;));
      fs.Close();
      fs.Dispose();
    }
    FileStream fsa = File.Open(targetPath, FileMode.OpenOrCreate);
    fsa.Write(b,0, b.Length);
    fsa.Close();
    _connection = new SQLiteConnection(targetPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
      CreateDB();
}
</code></pre>

<p>所以正确的路径似乎是:</p>

<pre><code> string bpa = Application.dataPath + &#34;/Raw/&#34; + StaticDatas.databaseName;
</code></pre>

<p><strong>------------------------------------------ --------------------</strong></p>

<p>更新 4:</p>

<p>好的,我想我知道问题出在哪里(即使我不明白):</p>

<p>我现在可以从流媒体 Assets 中获取数据并将其复制到persistentDatapath,但是当我创建与文件的连接时,SQLite 会抛出异常:</p>

<pre><code>Could not open database file
</code></pre>

<p>有人知道为什么吗?</p>

<p><strong>------------------------------------------ --------------------</strong>
更新 5:</p>

<p>我做了一个组合来创建路径“targetPath”,在日志中,它显示“/var/mobile/Container/Data/Application/manylettersanddigits/Documents/database.db”</p>

<p>但是,在 SQLException 中,它显示相同,但​​ Documents 和 database.db 之间没有斜线</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在 IOS 上,可以通过以下方式访问流媒体 Assets 的当前路径:</p>
<pre><code>Application.dataPath + &#34;/Raw/&#34; + StaticDatas.databaseName;
</code></pre>
<p>在这种情况下,“无法打开数据库文件”是因为我在方法中创建了 2 个连接(我感到羞耻)
事实上,第二个是让它成为错误(两者的变量相同),这是我的一个错误。</p>
<p>谢谢大家。</p></p>
                                   
                                                <p style="font-size: 20px;">关于c# - unity - IOS persistentDataPath access denied//无法打开数据库,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42245393/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42245393/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c# - unity - IOS persistentDataPath access denied//无法打开数据库