gpt4 book ai didi

c# - Unity - IOS persistentDataPath 访问被拒绝//无法打开数据库

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:14:24 27 4
gpt4 key购买 nike

我正在尝试从我的 streamingAssets 中恢复一个文件并在 persistentDataPath 中制作一个副本。

问题是访问 persistentDataPath 被 iOS 拒绝,所以我不能写文件,有人能告诉我为什么吗?

代码:

#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();
}

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

iOS: 9.5.3统一:5.5.0f3

-------------------------------------------- --------------------

更新:StaticDatas.databasePath = persistentDataPath

-------------------------------------------- --------------------

更新 2:

好吧,我完全迷路了......

这是我的代码:

#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 语句时,它说“Could not find a part of the path”(即使它在 if 语句中找到了它,卧槽?)

-------------------------------------------- --------------------

更新 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:

好吧,我想我知道问题是什么(即使我不明白):

我现在可以从流式 Assets 中获取我的数据并将其复制到持久数据路径,但是当我创建与文件的连接时,SQLite 抛出异常:

Could not open database file

有人知道为什么吗?

-------------------------------------------- ----------------------更新 5:

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

然而,在 SQLException 中,它显示相同但没有 Documents 和 database.db 之间的斜线

最佳答案

在 IOS 上,可以通过以下方式访问流媒体 Assets 的当前路径:

Application.dataPath + "/Raw/" + StaticDatas.databaseName;

在这种情况下,“无法打开数据库文件”是因为我在方法中创建了 2 个连接(我真可耻)事实上,第二个是让它变得错误(两者的变量相同),这是我的一个错误。

感谢大家。

关于c# - Unity - IOS persistentDataPath 访问被拒绝//无法打开数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42245393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com