ios - FMDatabase: connect DB made in advance -
i made dbfile sample.db
terminal in advance, , added file project.
app create dbfile scratch, instead of refering db added.
how need fix refer db added?
nsarray* paths = nssearchpathfordirectoriesindomains( nsdocumentdirectory, nsuserdomainmask, yes ); nsstring* dir = [paths objectatindex:0]; fmdatabase* db = [fmdatabase databasewithpath:[dir stringbyappendingpathcomponent:samble.db]]; [db open];
check existence of database in documents folder before try open it, , if it's not there, copy version bundle documents folder before subsequently opening documents folder.
so, first, because have blank database sitting in documents folder, remove (by uninstalling app , re-installing). , can following
bool success; nsarray* paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring* dir = [paths objectatindex:0]; nsstring* documentspath = [dir stringbyappendingpathcomponent:@"samble.db"]; nsfilemanager* filemanager = [nsfilemanager defaultmanager]; if (![filemanager fileexistsatpath:documentspath]) { nsstring *bundlepath = [[nsbundle mainbundle] pathforresource:@"samble" oftype:@"db"]; nsassert(bundlepath, @"%s: database not found in bundle", __function__); nserror *copyerror; success = [filemanager copyitematpath:bundlepath topath:documentspath error:©error]; nsassert(success, @"%s: copyitematpath failed: %@", __function__, copyerror); } fmdatabase* db = [fmdatabase databasewithpath:documentspath]; nsassert(db, @"%s: databasewithpath failed", __function__); success = [db open]; nsassert(success, @"%s: open failed", __function__);
Comments
Post a Comment