ios - replace / load database on iphone -
my simulator runs fine , fast. iphone seems freezing @ part try create , fill database. prefer use database simulator , put on iphone user doesn't have recreate database. know how can load database added folders.
i searched lot either outdated or different want. added database file finder xcode project. if i'm correct have change _databasepath point wherever file is, correct?
and if it, 1 code here: /var/mobile/applications/65b5541a-1e73-46f6-ab5a-c5988003103e/documents/paths.db no 1 dragged xcode. looked @ organizer, can see there documents/paths.db since misses other files assume that code created db , not dragged in. tried delete can't select it.
can help?
in header:
@property (strong, nonatomic) nsstring *databasepath; @property (nonatomic) sqlite3 *pathdb;
in .m:
- (void) createdatabaseifnotexist { nsstring *docsdir; nsarray *dirpaths; // documents directory dirpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); docsdir = dirpaths[0]; // build path database file _databasepath = [[nsstring alloc] initwithstring: [docsdir stringbyappendingpathcomponent:@"paths.db"]]; nslog(@"databasepath: %@", _databasepath); nsfilemanager *filemgr = [nsfilemanager defaultmanager]; if ([filemgr fileexistsatpath: _databasepath] == no) { const char *dbpath = [_databasepath utf8string]; if(sqlite3_open(dbpath, &_pathdb) == sqlite_ok) { char *errmsg; const char *sql_stmt = "create table if not exists paths (id integer primary key autoincrement, start integer, end integer, distance real, nodes text)"; if (sqlite3_exec(_pathdb, sql_stmt, null, null, &errmsg) != sqlite_ok) { //_status.text = @"failed create table"; nslog(@"failed create table"); } sqlite3_close(_pathdb); } else { // _status.text = @"failed open/create database"; nslog(@"failed open/create database"); } } }
so, couple of things:
you first need modify
createdatabaseifnotexist
copy bundle if it's not found indocuments
:- (void) createdatabaseifnotexist { // documents database path nsstring *docsdir = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes)[0]; self.databasepath = [docsdir stringbyappendingpathcomponent:@"paths.db"]; // use setter when setting property's value nsfilemanager *filemanager = [nsfilemanager defaultmanager]; if ([filemanager fileexistsatpath:_databasepath] == no) { // if database doesn't exist in documents, in bundle , copy if found // bundle database path nsstring *bundledatabasepath = [[nsbundle mainbundle] pathforresource:@"paths" oftype:@"db"]; if (bundledatabasepath) { // if copied bundle, quit if ([filemanager copyitematpath:bundledatabasepath topath:self.databasepath error:nil]) return; } // otherwise, let's proceed creating database if(sqlite3_open([_databasepath utf8string], &_pathdb) == sqlite_ok) { char *errmsg; const char *sql_stmt = "create table if not exists paths (id integer primary key autoincrement, start integer, end integer, distance real, nodes text)"; if (sqlite3_exec(_pathdb, sql_stmt, null, null, &errmsg) != sqlite_ok) { //_status.text = @"failed create table"; nslog(@"failed create table, %s", errmsg); sqlite3_free(errmsg); // if you're going use fifth parameter, must free when you're done } sqlite3_close(_pathdb); } else { // _status.text = @"failed open/create database"; nslog(@"failed open/create database"); } } }
second, once you've run once on simulator, find database in simulator's
documents
folder xcode project. simulator's files can found in~/library/application support/iphone simulator/6.1/applications/xxx/documents
where
xxx
cryptic identifier (e.g.85206ba6-9d03-4f18-bb0a-3b8c25b552c4
). note, default,library
folder hidden, go terminal command line , type in following command show it:chflags nohidden library
you can add database project dragging finder xcode's file navigator window, @ point you'll see window like:
make sure check 2 highlighted checkmarks ensure database included in bundle.
two final observations:
now have "copy bundle if necessary logic", it's interesting question whether want code create table in code @ anymore. personally, create databases nice mac graphical sqlite tool , copy them project. time programmatic creating of tables when (a) i'm deploying update involves new/altered tables; , (b) user's database might contain key data don't want replace database bundle.
i include
configuration
table in app contains single row 1 of columns database version. thus, app open database documents, check version number, , if out of date (because user upgraded app) update database. "database version number" logic want in place part of version 1.0 of app.
Comments
Post a Comment