ios - Read local sqlite file -
i need read local sqlite file on ipad, not need queries how open it, starting point. downloaded file ftp server , located in app.
opening , closing sqlite database not hard. need follow few steps:
step 1 - copy database out of bundle app's documents folder.
you don't want tinkering bundle version of database, want use specific instance this device.
//******************************************* - (void)copydatabasetodocumentsfolder { nsstring *documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *localdatabase = [documentspath stringbyappendingpathcomponent: @"yourdatabasefilename.sql"]; bool fileexists = [[nsfilemanager defaultmanager] fileexistsatpath: localdatabase]; if (fileexists == no) { nserror *err; // path database in application package nsstring *databaseinappbundle = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent: @"yourdatabasefilename.sql"]; // copy file package users filesystem if (![[nsfilemanager defaultmanager] copyitematpath: databaseinappbundle topath: localdatabase error: &err]) { nslog(@"database copy failed: %@", [err description]); } } }
step 2: - create ivar database.
@interface yourclass () { sqlite3 *localdb; } @end
step 3 - open , close needed.
//******************************************* - (void)closedatabase { if (localdb != nil) { sqlite3_close(localdb); localdb = nil; } } //******************************************* - (void)opendatabase { if (localdb == nil) { nsstring *documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; char *dbpath = (char *)[[documentspath stringbyappendingpathcomponent: @"yourdatabasefilename.sql"] utf8string]; sqlite3_open(dbpath, &localdb); } }
Comments
Post a Comment