objective c - Error for open connect of database with FMDB -
when i'm going open connect database, console says: "error opening!: 14". included "mybase.sqlite" on folder resources of project , i'm using fmdb framework.
for open connect i'm using code:
nsautoreleasepool * pool = [[nsautoreleasepool alloc] init]; fmdatabase* db = [fmdatabase databasewithpath:@"/mybase.sqlite"]; if (![db open]) { nslog(@"não abriu o banco de dados."); [pool release]; return 0; }
in appdelegate, included code:
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { // override point customization after application launch. homeviewcontroller *homevc = [[homeviewcontroller alloc] init]; navigationcontroller = [[uinavigationcontroller alloc] initwithrootviewcontroller:homevc]; [self createeditablecopyofdatabaseifneeded]; [window addsubview:navigationcontroller.view]; [window makekeyandvisible]; return yes; } - (void)createeditablecopyofdatabaseifneeded{ bool success; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nserror *error; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *writabledbpath = [documentsdirectory stringbyappendingpathcomponent:@"mybase.sqlite"]; success = [filemanager fileexistsatpath:writabledbpath]; nslog(@"success %d", success); if (success) return; nsstring *defaultdbpath = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@"mybase.sqlite"]; success = [filemanager copyitematpath:defaultdbpath topath:writabledbpath error:&error]; if (!success) { nsassert1(0, @"failed create writable database file message '%@'.", [error localizeddescription]); } }
i think open path incorrect. specifying path doesn't make sense, if db file in root folder.
fmdatabase* db = [fmdatabase databasewithpath:@"/mybase.sqlite"];
the above should use code file path, have in question.
nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *writabledbpath = [documentsdirectory stringbyappendingpathcomponent:@"mybase.sqlite"]; fmdatabase* db = [fmdatabase databasewithpath:writabledbpath];
Comments
Post a Comment