title: Unity - IOS persistentDataPath access denied // Could not open database
body: <p>I am trying to recover a file from my streamingAssets and make a copy of it in the persistentDataPath.</p>
  
  <p>The problem is that access to persistentDataPath is denied by iOS, so I cannot write the file, can someone tell me why?</p>
  
  <p>Code:</p>
  
  <pre><code>#elif UNITY_IOS
  if (File.Exists(Application.dataPath + "/Raw/" + StaticDatas.databaseName))
  {
      byte[] bytes = null;
         if (File.Exists(Application.dataPath + "/Raw/" + StaticDatas.databaseName))
         {
          Debug.Log(Application.dataPath + "/Raw/" + StaticDatas.databaseName);
          Debug.Log(StaticDatas.databasePath + StaticDatas.databaseName);
             using (FileStream fileStream = File.OpenRead(Application.dataPath + "/Raw/" + StaticDatas.databaseName))
             {
                 bytes = new byte[fileStream.Length];
                 fileStream.Read(bytes,0,int.Parse(fileStream.Length+""));
                 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>Error:
  Access to the path /var/mobile/containers/dta/application/manythings/DocumentsDatabaseName.db " is denied</p>
  
  <p>IOS: 9.5.3
  Unity: 5.5.0f3</p>
  
  <p><strong>----------------------------------------------------------------</strong></p>
  
  <p>UPDATE: StaticDatas.databasePath = persistentDataPath</p>
  
  <p><strong>----------------------------------------------------------------</strong></p>
  
  <p>UPDATE 2:</p>
  
  <p>Well, I am completely lost...</p>
  
  <p>Here is the code I have:</p>
  
  <pre><code>#elif UNITY_IOS
  
  string basePath = Path.Combine(Application.dataPath + "Raw" , 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("base path exists");
             using (FileStream fileStream = File.OpenRead(basePath))
             {
                 Debug.Log("create byte array");
                 bytes = new byte[fileStream.Length];
                 Debug.Log(" READ BYTES");
                 fileStream.Read(bytes,0,int.Parse(fileStream.Length+""));
                 Debug.Log(" CLOSE");
                 fileStream.Close();
                 Debug.Log("DISPOSE");
                 fileStream.Dispose();
             }
             Debug.Log(" Check if dir exists");
            /* if (!Directory.Exists(StaticDatas.databasePath + "/AnnotationTest.app/database/"))
             {
                  Debug.Log(" create folder");
                  Directory.CreateDirectory(StaticDatas.databasePath + "/AnnotationTest.app/database/");
             }*/
             Debug.Log("Open file");
             FileStream fs = File.Open(targetPath, FileMode.OpenOrCreate);
           Debug.Log("Write file");
             fs.Write(bytes, 0, bytes.Length);
             Debug.Log(" CLOSE STRREAM");
             fs.Close();
         Debug.Log("Connec close");
           _connection = new SQLiteConnection(targetPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
           Debug.Log("sql connect");
          CreateDB();
          Debug.Log(" db made");
  }
  
  
  
  if (File.Exists("file:" + Application.dataPath + "/Raw/" + StaticDatas.databaseName));
  {
      Debug.Log("file datapath raw databasename");
            byte[] bytes = null;
  
          Debug.Log("base path exists");
             using (FileStream fileStream = File.OpenRead("file:" + Application.dataPath + "/Raw/" + StaticDatas.databaseName))
             {
                 Debug.Log("create byte array");
                 bytes = new byte[fileStream.Length];
                 Debug.Log(" READ BYTES");
                 fileStream.Read(bytes,0,int.Parse(fileStream.Length+""));
                 Debug.Log(" CLOSE");
                 fileStream.Close();
                 Debug.Log("DISPOSE");
                 fileStream.Dispose();
             }
              FileStream fs = File.Open(targetPath, FileMode.OpenOrCreate);
           Debug.Log("Write file");
             fs.Write(bytes, 0, bytes.Length);
             Debug.Log(" CLOSE STRREAM");
             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>The first if statement did work for a moment (without <code>combine</code>), but now, peanuts.</p>
  
  <p>The second one returns true (<code>Exists</code>), but when it reaches the using statement, it says "Could not find a part of the path" (Even if it finds it in the if statement, wtf?)</p>
  
  <p><strong>----------------------------------------------------------------</strong></p>
  
  <p>UPDATE 3</p>
  
  <p>It's a miracle! I found it!
  Wel, now, SQLite could not open the database, but i found it!</p>
  
  <pre><code>string bpa = Application.dataPath + "/Raw/" + StaticDatas.databaseName;
  
  if (File.Exists(bpa))
  {
      byte[] b = null;
      using (FileStream fs = File.OpenRead(bpa))
      {
          b = new byte[fs.Length];
          fs.Read(b, 0, int.Parse(fs.Length+""));
          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.Length];
          fs.Read(b, 0, int.Parse(fs.Length+""));
          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>So the correct pathseems to be :</p>
  
  <pre><code> string bpa = Application.dataPath + "/Raw/" + StaticDatas.databaseName;
  </code></pre>
  
  <p><strong>----------------------------------------------------------------</strong></p>
  
  <p>UPDATE 4:</p>
  
  <p>Ok, I think I know what the problem is (even if I don't understand it):</p>
  
  <p>I can now get my datas from the streaming assets and copy it to the persistentDatapath, but when I create a connection with the file, SQLite throws an exception:</p>
  
  <pre><code>Could not open database file
  </code></pre>
  
  <p>Does someone know why?</p>
  
  <p><strong>----------------------------------------------------------------</strong>
  UPDATE 5:</p>
  
  <p>I do a Combine to create the path "targetPath", in the logs, it shows "/var/mobile/Container/Data/Application/manylettersanddigits/Documents/database.db"</p>
  
  <p>However, in the SQLException, it shows the same but without the slash between Documents and database.db</p>