objective c - How should I specify the path for an sqlite db in an iPhone project? -
after adding resource, database file in project root.
i've been able open specifying full path os x sees it, i.e., "/users/louis/documents/test project/test.db".
but of course there no such path on iphone.
i think should define path "application root/test.db" don't know how, or if work anywhere else besides development machine.
thanks ideas.
to path of file you've added in xcode use pathforresource:oftype: mainbundle.
nsstring *path = [[nsbundle mainbundle] pathforresource:@"yourdb" oftype:@"sqlite"];
but can't change files in mainbundle. have copy location. example library of app.
you this:
nsstring *librarypath = [nssearchpathfordirectoriesindomains(nslibrarydirectory, nsuserdomainmask, yes) lastobject]; nsstring *targetpath = [librarypath stringbyappendingpathcomponent:@"yourdb.sqlite"]; if (![[nsfilemanager defaultmanager] fileexistsatpath:targetpath]) { // database doesn't exist in library path... copy bundle nsstring *sourcepath = [[nsbundle mainbundle] pathforresource:@"yourdb" oftype:@"sqlite"]; nserror *error = nil; if (![[nsfilemanager defaultmanager] copyitematpath:sourcepath topath:targetpath error:&error]) { nslog(@"error: %@", error); } }
Comments
Post a Comment