C# Unity-IOS persistentDataPath访问被拒绝//无法打开数据库
我正在尝试从streamingAssets中恢复文件,并在persistentDataPath中复制该文件 问题是iOS拒绝访问persistentDataPath,因此我无法写入文件,有人能告诉我为什么吗 代码: 错误: 拒绝访问路径/var/mobile/containers/dta/application/manythings/DocumentsDatabaseName.db” IOS:9.5.3 统一:5.5.0f3 --------------------------------------------------------------- 更新:StaticDatas.databasePath=persistentDataPath --------------------------------------------------------------- 更新2: 嗯,我完全迷路了 以下是我的代码:C# Unity-IOS persistentDataPath访问被拒绝//无法打开数据库,c#,ios,unity3d,C#,Ios,Unity3d,我正在尝试从streamingAssets中恢复文件,并在persistentDataPath中复制该文件 问题是iOS拒绝访问persistentDataPath,因此我无法写入文件,有人能告诉我为什么吗 代码: 错误: 拒绝访问路径/var/mobile/containers/dta/application/manythings/DocumentsDatabaseName.db” IOS:9.5.3 统一:5.5.0f3 -----------------------------------
#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
第一个if语句确实工作了一段时间(没有combine
),但现在,它只工作了一小会儿
第二个返回true(Exists
),但是当它到达using语句时,它说“找不到路径的一部分”(即使它在if语句中找到它,wtf?)
---------------------------------------------------------------
更新3
真是奇迹!我找到了!
嗯,现在,SQLite无法打开数据库,但我找到了
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();
}
因此,正确的路径似乎是:
string bpa = Application.dataPath + "/Raw/" + StaticDatas.databaseName;
---------------------------------------------------------------
更新4:
好的,我想我知道问题是什么(即使我不明白):
现在,我可以从流资产获取数据并将其复制到persistentDatapath,但当我创建与该文件的连接时,SQLite会引发异常:
Could not open database file
有人知道为什么吗
---------------------------------------------------------------
更新5:
我结合创建路径“targetPath”,在日志中显示“/var/mobile/Container/Data/Application/manylettersanddights/Documents/database.db”
但是,在SQLException中,它显示了相同的内容,但文档和数据库之间没有斜杠。在IOS上,可以通过以下方式访问流媒体资产的相应路径:
Application.dataPath + "/Raw/" + StaticDatas.databaseName;
在本例中,之所以出现“无法打开数据库文件”,是因为我在方法中创建了2个连接(真丢脸)
事实上,第二个是buggig(两者的变量相同),这是我的错误
谢谢大家。的副本?会看的,谢谢我很困惑…你为什么问
应用程序。persistantDataPath
,但是在你的代码中你使用的是应用程序。dataPath?
我很确定这回答了你的问题。是的,我的错,我忘了提到Static.databasePath是一个静态字符串istentDataPath。(在创建文件时使用)我链接的解决方案有效吗?
Application.dataPath + "/Raw/" + StaticDatas.databaseName;