我只能通过指定OS X看到它的完整路径来打开它,即“/ Users / Louis / documents / Test Project / test.db”.
但当然iPhone上没有这样的路径.
我想我应该将路径定义为“application root / test.db”,但我不知道如何,或者除了我的开发机器之外还能在其他地方工作.
谢谢你的任何想法.
解决方法 要获取您在XCode中添加的文件的路径,您将使用pathForResource:ofType:与您的mainBundle.Nsstring *path = [[NSBundle mainBundle] pathForResource:@"yourDb" ofType:@"sqlite"];
但是您无法更改mainBundle中的文件.所以你必须将它复制到另一个位置.例如,您的应用程序库.
你可以这样做:
Nsstring *libraryPath = [NSSearchPathForDirectorIEsInDomains(NSlibraryDirectory,NSUserDomainMask,YES) lastObject];Nsstring *targetPath = [libraryPath stringByAppendingPathComponent:@"yourDB.sqlite"];if (![[NSfileManager defaultManager] fileExistsAtPath:targetPath]) { // database doesn't exist in your library path... copy it from the bundle Nsstring *sourcePath = [[NSBundle mainBundle] pathForResource:@"yourDb" ofType:@"sqlite"]; NSError *error = nil; if (![[NSfileManager defaultManager] copyItemAtPath:sourcePath topath:targetPath error:&error]) { NSLog(@"Error: %@",error); }}总结
以上是内存溢出为你收集整理的我应该如何在iPhone项目中指定sqlite数据库的路径?全部内容,希望文章能够帮你解决我应该如何在iPhone项目中指定sqlite数据库的路径?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)