2011-11-07 14 views
23

Buduję aplikację, w której chciałbym lokalnie przechowywać dane użytkownika na urządzeniu, bez korzystania z bazy danych serwera - ale wszystko po stronie urządzenia. Staram się przechowywać określone lokalizacje użytkowników i wyświetlać je w widoku tabeli, więc nawet jeśli użytkownik uruchomi aplikację później - mogę pobrać historię i wyświetlić tabelę historii z wcześniejszymi lokalizacjami. Zasadniczo możliwość odczytu/zapisu z lokalnej bazy danych.Przechowywanie danych lokalnie na iPhone

Wiem, że wcześniej było wiele takich pytań, ale nie mogłem znaleźć takiego, który adresowałby dane do zapisu lokalnie bez zewnętrznej bazy danych. Nie jestem pewien, na przykład, że używanie Core Data jest właściwą i najprostszą rzeczą do zrobienia tutaj.

Byłbym wdzięczny za wszelkie porady na ten temat.

+0

Sprawdź to pytanie/odpowiedź: http: // stackoverflow .do om/questions/307313/best-way-to-save-data-on-the-iphone – Ralfonso

+0

Jeśli problem został rozwiązany, sprawdź pytanie jako rozwiązane i wybierz odpowiedź, aby pomóc ludziom w przyszłości z tym samym problemem. –

+0

hej @TommyG przechowując rekordy transakcyjne, w swojej lokalizacji lepiej jest używać zewnętrznej bazy danych, takiej jak SqLite lub CoreDate, niż używać NSUserDefaults [https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/ NSUserDefaults_Class /]. Aby ułatwić sobie życie, możesz wypróbować bibliotekę ORM, taką jak https://github.com/LakithaRav/OLCOrm, aby wykonać zadanie. – Laky

Odpowiedz

63

do prostego danych należy użyć NSUserDefaults. CoreData jest bardzo fajna, ale głównie do przechowywania struktur DB i wprowadza złożoność (ale uwielbiam to :)). Jeśli wystarczy przechowywać String, array i tak dalej (w zasadzie preferencje), można przejść z NSUserDefaults:

Na przykład:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; //load NSUserDefaults 
NSArray *fakeFavs = [[ NSArray alloc] initWithObjects:@"2",@"4", @"100", nil]; //declare array to be stored in NSUserDefaults 
[prefs setObject:fakeFavs forKey:@"favourites"]; //set the prev Array for key value "favourites" 
+1

dzięki! jak mogę odczytać tę tablicę po ponownym uruchomieniu aplikacji? – TommyG

+12

NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults]; NSMutableArray * favorites = [prefs arrayForKey: @ "favorites"]; –

+1

Jeśli pomogliśmy ci, nie zapomnij głosować! :) –

10

Masz w zasadzie dwie opcje przechowywania danych:

  1. CoreData (jeśli planujesz używać nowsze wersje iOS)
  2. SQLite (obsługuje dowolną wersję SDK)

CoreData używa SQLite, jego interfejs API jest nieco łatwiejszy w użyciu (nie musisz znać języka SQL ani pisać wielu funkcji do odczytu i zapisu danych).

SQLite API jest wciąż świetnym wyborem, ponieważ używa API C dla SQLite, który jest bardzo dobrze udokumentowany i prosty w użyciu. Ma to tę zaletę, że można w ten sposób kierować na starsze platformy iOS.

W przypadku obu opcji dane będą przechowywane po stronie klienta i będą zabezpieczane za każdym razem, gdy użytkownik zsynchronizuje swój telefon z iTunes.

+0

czy SQLite nie jest używany, gdy masz bazę danych na serwerze? Muszę przechowywać lokalnie tylko jeden obiekt i czytać go/zapisywać. – TommyG

+0

@TommyG: Nie. Nawet gdybyś mógł technicznie użyć SQLite na serwerze, bardzo zniechęciłbym cię do robienia tego. Brakuje dobrego, współbieżnego wsparcia dostępu. SQLite to mobilny interfejs API bazy danych przeznaczony do użytku w środowiskach pojedynczego użytkownika, takich jak iPhone, telefony z systemem Android lub proste aplikacje komputerowe. CoreData używa go za sceną. –

+0

fajne, dzięki temu spróbuję! – TommyG

4

jeśli tylko kilka przechowywania wartości i nie potrzebują żadnej logiki wyszukiwania, można spojrzeć na NSUserDefaults

[NSUserDefaults standardUserDefaults] 

jej tylko słownik, w którym można przechowywać tablice, ciągi, int, obiektów i Dostęp przez NSString klucz

wewnętrznie jest to tylko plist, dzięki czemu można go otworzyć z Xcode do quicky zobaczyć aktualny stan

+0

Czy mogę napisać do niej tablicę ciągów, a następnie przeczytać ją następnym razem, gdy użytkownik otworzy aplikację? to jest to, czego zasadniczo potrzebuję. zajrzałem do podstawowych danych, i szczerze mówiąc na to, czego potrzebuję na przesadę ... – TommyG

+0

Tak, możesz napisać tablicę łańcuchów. [[NSUserDefaults standardUserDefaults] setObject: myArray forKey: @ "myKey"], a następnie odczytaj go za pomocą NSArray * myArray = [[NSUserDefaults standardUserDefaults] objectForKey: @ "myKey"]; – EricS

+0

tak i jest utrzymywany automatycznie. Oznacza to, że jeśli użytkownik lub ios zamknie aplikację, twoje dane nadal tam są. a także zrobione przez iTunes – JeanLuc

0
Select Query:::::: 

NSArray *array_hiback =[app.sk lookupAllForSQL:@"SELECT * FROM cycle_datagathering_begoodyourselef ORDER BY RANDOM() LIMIT 1"]; 
    NSLog(@"%@",array_hiback);  

insert Query::::: 

NSMutableDictionary *dict=[[NSMutableDictionary alloc]init]; 
[dict setObject:@"0" forKey:@"isuploaded"]; 
     [app.sk insertDictionary:dict forTable:@"mindeditor"]; 
     [dict release]; 


update Query:::: 


    NSMutableDictionary *updatedata=[[NSMutableDictionary alloc]init]; 
[updatedata setObject: [[NSUserDefaults standardUserDefaults] objectForKey:@"savecycleid"] forKey:@"cycleid"]; 


delete Query::::: 

     [app.sk deleteWhere:[NSString stringWithFormat:@"rowid=%@",str] forTable:@"mindeditor"]; 

      [app.sk updateDictionary:updatedata forTable:@"mindeditor" where:[NSString stringWithFormat:@"rowid=%@", [[checkarray objectAtIndex:checkarray.count-1] objectForKey:@"cycleid"]]]; 
      [updatedata release]; 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
     { 


      sk = [[SKDatabase alloc] init]; 
      NSString *db = @"MindEditor.db"; 
      [sk initWithDynamicFile:db]; 
      [email protected]"0"; 
     } 


     SKDatabase.h 


    // 
    // SKDatabase.h 
    // Version 1.1 
    // 
    // Created by Shannon Appelcline on 9/11/08. 
    // Copyright 2008 __MyCompanyName__. All rights reserved. 
    // 

    #import <UIKit/UIKit.h> 
    #import <sqlite3.h> 

    @protocol SKDatabaseDelegate <NSObject> 
    @optional 
    - (void)databaseTableWasUpdated:(NSString *)table; 
    @end 

    @interface SKDatabase : NSObject { 

     id<SKDatabaseDelegate> delegate; 
     sqlite3 *dbh; 
     BOOL dynamic; 
    } 

    @property (assign) id<SKDatabaseDelegate> delegate; 
    @property sqlite3 *dbh; 
    @property BOOL dynamic; 

    - (id)initWithFile:(NSString *)dbFile; 
    - (id)initWithDynamicFile:(NSString *)dbFile; 
    - (void)close; 

    - (sqlite3_stmt *)prepare:(NSString *)sql; 

    - (id)lookupColForSQL:(NSString *)sql; 
    - (NSDictionary *)lookupRowForSQL:(NSString *)sql; 
    - (NSArray *)lookupAllForSQL:(NSString *)sql; 

    - (int)lookupCountWhere:(NSString *)where forTable:(NSString *)table; 
    - (int)lookupMax:(NSString *)key Where:(NSString *)where forTable:(NSString *)table; 
    - (int)lookupSum:(NSString *)key Where:(NSString *)where forTable:(NSString *)table; 

    - (void)insertArray:(NSArray *)dbData forTable:(NSString *)table; 
    - (void)insertDictionary:(NSDictionary *)dbData forTable:(NSString *)table; 

    - (void)updateArray:(NSArray *)dbData forTable:(NSString *)table; 
    - (void)updateArray:(NSArray *)dbData forTable:(NSString *)table where:(NSString *)where; 
    - (void)updateDictionary:(NSDictionary *)dbData forTable:(NSString *)table; 
    - (void)updateDictionary:(NSDictionary *)dbData forTable:(NSString *)table where:(NSString *)where; 
    - (void)updateSQL:(NSString *)sql forTable:(NSString *)table; 

    - (void)deleteWhere:(NSString *)where forTable:(NSString *)table; 

    - (BOOL)runDynamicSQL:(NSString *)sql forTable:(NSString *)table; 

    @end 

     SKDatabase.m 




    // 
    // SKDatabase.m 
    // Version 1.1 
    // 
    // Created by Shannon Appelcline on 9/11/08. 
    // Copyright 2008 __MyCompanyName__. All rights reserved. 
    // 

    #import "SKDatabase.h" 

    @implementation SKDatabase 

    @synthesize delegate; 
    @synthesize dbh; 
    @synthesize dynamic; 

    // Two ways to init: one if you're just SELECTing from a database, one if you're UPDATing 
    // and or INSERTing 

    - (id)initWithFile:(NSString *)dbFile { 
     if (self = [super init]) { 

      NSString *paths = [[NSBundle mainBundle] resourcePath]; 
      NSString *path = [paths stringByAppendingPathComponent:dbFile]; 

      int result = sqlite3_open([path UTF8String], &dbh); 
      NSAssert1(SQLITE_OK == result, NSLocalizedStringFromTable(@"Unable to open the sqlite database (%@).", @"Database", @""), [NSString stringWithUTF8String:sqlite3_errmsg(dbh)]); 
      self.dynamic = NO; 
     } 

     return self;  
    } 

    - (id)initWithDynamicFile:(NSString *)dbFile { 
     if (self = [super init]) { 

      NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *docDir = [docPaths objectAtIndex:0]; 
      NSString *docPath = [docDir stringByAppendingPathComponent:dbFile]; 

      NSFileManager *fileManager = [NSFileManager defaultManager]; 

      if (![fileManager fileExistsAtPath:docPath]) { 

       NSString *origPaths = [[NSBundle mainBundle] resourcePath]; 
       NSString *origPath = [origPaths stringByAppendingPathComponent:dbFile]; 

       NSError *error; 
       int success = [fileManager copyItemAtPath:origPath toPath:docPath error:&error];    
       NSAssert1(success,[NSString stringWithString:@"Failed to copy database into dynamic location"],error); 
      } 
      int result = sqlite3_open([docPath UTF8String], &dbh); 
      NSAssert1(SQLITE_OK == result, NSLocalizedStringFromTable(@"Unable to open the sqlite database (%@).", @"Database", @""), [NSString stringWithUTF8String:sqlite3_errmsg(dbh)]); 
      self.dynamic = YES; 
     } 

     return self;  
    } 

    // Users should never need to call prepare 

    - (sqlite3_stmt *)prepare:(NSString *)sql { 

     const char *utfsql = [sql UTF8String]; 

     sqlite3_stmt *statement; 

     if (sqlite3_prepare([self dbh],utfsql,-1,&statement,NULL) == SQLITE_OK) { 
      return statement; 
     } else { 
      return 0; 
     } 
    } 

    // Three ways to lookup results: for a variable number of responses, for a full row 
    // of responses, or for a singular bit of data 

    - (NSArray *)lookupAllForSQL:(NSString *)sql { 
     sqlite3_stmt *statement; 
     id result; 
     NSMutableArray *thisArray = [NSMutableArray arrayWithCapacity:4]; 
     if (statement = [self prepare:sql]) { 
      while (sqlite3_step(statement) == SQLITE_ROW) { 
       NSMutableDictionary *thisDict = [NSMutableDictionary dictionaryWithCapacity:4]; 
       for (int i = 0 ; i < sqlite3_column_count(statement) ; i++) 
       { 
        if (sqlite3_column_decltype(statement,i) != NULL && 
         strcasecmp(sqlite3_column_decltype(statement,i),"Boolean") == 0) 
        { 
         result = [NSNumber numberWithBool:(BOOL)sqlite3_column_int(statement,i)]; 
        } 
        else if (sqlite3_column_type(statement, i) == SQLITE_TEXT) 
        { 
         result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,i)]; 
        } 
        else if 
         (sqlite3_column_type(statement,i) == SQLITE_INTEGER) 
        { 
         result = [NSNumber numberWithInt:(int)sqlite3_column_int(statement,i)]; 
        } 
        else if (sqlite3_column_type(statement,i) == SQLITE_FLOAT) 
        { 
         result = [NSNumber numberWithFloat:(float)sqlite3_column_double(statement,i)];     
        } 
        else 
        { 
         result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,i)]; 
        } 
        if (result) 
        { 
         [thisDict setObject:result 
            forKey:[NSString stringWithUTF8String:sqlite3_column_name(statement,i)]]; 
        } 
       } 
       [thisArray addObject:[NSDictionary dictionaryWithDictionary:thisDict]]; 
       [thisArray retain]; 
      } 
     } 
     sqlite3_finalize(statement); 
     return thisArray; 
    } 

    - (NSDictionary *)lookupRowForSQL:(NSString *)sql { 
     sqlite3_stmt *statement; 
     id result; 
     NSMutableDictionary *thisDict = [NSMutableDictionary dictionaryWithCapacity:4]; 
     if (statement = [self prepare:sql]) 
     { 
      if (sqlite3_step(statement) == SQLITE_ROW) 
      { 
       for (int i = 0 ; i < sqlite3_column_count(statement) ; i++) 
       { 
        if (strcasecmp(sqlite3_column_decltype(statement,i),"Boolean") == 0) 
        { 
         result = [NSNumber numberWithBool:(BOOL)sqlite3_column_int(statement,i)]; 
        } 
        else if (sqlite3_column_type(statement, i) == SQLITE_TEXT) 
        { 
         result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,i)]; 
        } 
        else if (sqlite3_column_type(statement,i) == SQLITE_INTEGER) 
        { 
         result = [NSNumber numberWithInt:(int)sqlite3_column_int(statement,i)]; 
        } 
        else if (sqlite3_column_type(statement,i) == SQLITE_FLOAT) 
        { 
         result = [NSNumber numberWithFloat:(float)sqlite3_column_double(statement,i)];     
        } 
        else 
        { 
         result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,i)]; 
        } 
        if (result) 
        { 
         [thisDict setObject:result 
            forKey:[NSString stringWithUTF8String:sqlite3_column_name(statement,i)]]; 
        } 
       } 
      } 
     } 
     sqlite3_finalize(statement); 
     return thisDict; 
    } 

    - (id)lookupColForSQL:(NSString *)sql { 

     sqlite3_stmt *statement; 
     id result; 
     if (statement = [self prepare:sql]) { 
      if (sqlite3_step(statement) == SQLITE_ROW) {   
       if (strcasecmp(sqlite3_column_decltype(statement,0),"Boolean") == 0) { 
        result = [NSNumber numberWithBool:(BOOL)sqlite3_column_int(statement,0)]; 
       } else if (sqlite3_column_type(statement, 0) == SQLITE_TEXT) { 
        result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]; 
       } else if (sqlite3_column_type(statement,0) == SQLITE_INTEGER) { 
        result = [NSNumber numberWithInt:(int)sqlite3_column_int(statement,0)]; 
       } else if (sqlite3_column_type(statement,0) == SQLITE_FLOAT) { 
        result = [NSNumber numberWithDouble:(double)sqlite3_column_double(statement,0)];      
       } else { 
        result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]; 
       } 
      } 
     } 
     sqlite3_finalize(statement); 
     return result; 

    } 

    // Simple use of COUNTS, MAX, etc. 

    - (int)lookupCountWhere:(NSString *)where forTable:(NSString *)table { 

     int tableCount = 0; 
     NSString *sql = [NSString stringWithFormat:@"SELECT COUNT(*) FROM %@ WHERE %@", 
         table,where];  
     sqlite3_stmt *statement; 

     if (statement = [self prepare:sql]) { 
      if (sqlite3_step(statement) == SQLITE_ROW) {   
       tableCount = sqlite3_column_int(statement,0); 
      } 
     } 
     sqlite3_finalize(statement); 
     return tableCount; 

    } 

    - (int)lookupMax:(NSString *)key Where:(NSString *)where forTable:(NSString *)table { 

     int tableMax = 0; 
     NSString *sql = [NSString stringWithFormat:@"SELECT MAX(%@) FROM %@ WHERE %@", 
         key,table,where];  
     sqlite3_stmt *statement; 
     if (statement = [self prepare:sql]) { 
      if (sqlite3_step(statement) == SQLITE_ROW) {   
       tableMax = sqlite3_column_int(statement,0); 
      } 
     } 
     sqlite3_finalize(statement); 
     return tableMax; 

    } 

    - (int)lookupSum:(NSString *)key Where:(NSString *)where forTable:(NSString *)table { 

     int tableSum = 0; 
     NSString *sql = [NSString stringWithFormat:@"SELECT SUM(%@) FROM %@ WHERE %@", 
         key,table,where];  
     sqlite3_stmt *statement; 
     if (statement = [self prepare:sql]) { 
      if (sqlite3_step(statement) == SQLITE_ROW) {   
       tableSum = sqlite3_column_int(statement,0); 
      } 
     } 
     sqlite3_finalize(statement); 
     return tableSum; 

    } 

    // INSERTing and UPDATing 

    - (void)insertArray:(NSArray *)dbData forTable:(NSString *)table { 

    // NSMutableString *sql = [NSMutableString stringWithCapacity:16]; 
    // [sql appendFormat:@"INSERT INTO %@ (",table]; 
    // 
    // 
    // for (int i = 0 ; i < [dbData count] ; i++) { 
    //  NSLog(@"%@",[[dbData objectAtIndex:i] objectForKey:@"mid"]); 
    //  [sql appendFormat:@"%@",[[dbData objectAtIndex:i] objectForKey:@"key"]]; 
    //  if (i + 1 < [dbData count]) { 
    //   [sql appendFormat:@", "]; 
    //  } 
    // } 
    // [sql appendFormat:@") VALUES("]; 
    // for (int i = 0 ; i < [dbData count] ; i++) { 
    //  if ([[[dbData objectAtIndex:i] objectForKey:@"value"] intValue]) { 
    //   [sql appendFormat:@"%@",[[[dbData objectAtIndex:i] objectForKey:@"value"] intValue]]; 
    //  } else { 
    //   [sql appendFormat:@"'%@'",[[dbData objectAtIndex:i] objectForKey:@"value"]]; 
    //  } 
    //  if (i + 1 < [dbData count]) { 
    //   [sql appendFormat:@", "]; 
    //  } 
    // } 
    // [sql appendFormat:@")"]; 
    // [self runDynamicSQL:sql forTable:table]; 
     for(int i=0;i<[dbData count];i++) 
     { 
      NSDictionary *dict=[dbData objectAtIndex:i]; 
      NSMutableString *sql = [NSMutableString stringWithCapacity:16]; 
      [sql appendFormat:@"INSERT INTO %@ (",table]; 

      NSArray *dataKeys = [dict allKeys]; 
      for (int i = 0 ; i < [dataKeys count] ; i++) { 
       [sql appendFormat:@"%@",[dataKeys objectAtIndex:i]]; 
       if (i + 1 < [dataKeys count]) { 
        [sql appendFormat:@", "]; 
       } 
      } 

      [sql appendFormat:@") VALUES("]; 
      for (int i = 0 ; i < [dataKeys count] ; i++) { 
       if ([[dict objectForKey:[dataKeys objectAtIndex:i]] intValue]) { 
        [sql appendFormat:@"%@",[dict objectForKey:[dataKeys objectAtIndex:i]]]; 
       } else { 
        [sql appendFormat:@"'%@'",[dict objectForKey:[dataKeys objectAtIndex:i]]]; 
       } 
       if (i + 1 < [dict count]) { 
        [sql appendFormat:@", "]; 
       } 
      } 

      [sql appendFormat:@")"]; 
      [self runDynamicSQL:sql forTable:table]; 
     } 
    } 

    - (void)insertDictionary:(NSDictionary *)dbData forTable:(NSString *)table { 

     NSMutableString *sql = [NSMutableString stringWithCapacity:16]; 
     [sql appendFormat:@"INSERT INTO %@ (",table]; 

     NSArray *dataKeys = [dbData allKeys]; 
     for (int i = 0 ; i < [dataKeys count] ; i++) { 
      [sql appendFormat:@"%@",[dataKeys objectAtIndex:i]]; 
      if (i + 1 < [dbData count]) { 
       [sql appendFormat:@", "]; 
      } 
     } 

     [sql appendFormat:@") VALUES("]; 
     for (int i = 0 ; i < [dataKeys count] ; i++) { 
      //if ([[dbData objectForKey:[dataKeys objectAtIndex:i]] intValue]) { 
    //   [sql appendFormat:@"%@",[dbData objectForKey:[dataKeys objectAtIndex:i]]]; 
    //  } else { 

      [sql appendFormat:@"'%@'",[dbData objectForKey:[dataKeys objectAtIndex:i]]]; 
      //} 
      if (i + 1 < [dbData count]) { 
       [sql appendFormat:@", "]; 
      } 
     } 

     [sql appendFormat:@")"]; 
     [self runDynamicSQL:sql forTable:table]; 
    } 

    - (void)updateArray:(NSArray *)dbData forTable:(NSString *)table { 
     [self updateArray:dbData forTable:table where:NULL]; 
    } 

    - (void)updateArray:(NSArray *)dbData forTable:(NSString *)table where:(NSString *)where { 

     NSMutableString *sql = [NSMutableString stringWithCapacity:16]; 
     [sql appendFormat:@"UPDATE %@ SET ",table]; 

     for (int i = 0 ; i < [dbData count] ; i++) { 
      if ([[[dbData objectAtIndex:i] objectForKey:@"value"] intValue]) { 
       [sql appendFormat:@"%@=%@", 
       [[dbData objectAtIndex:i] objectForKey:@"key"], 
       [[dbData objectAtIndex:i] objectForKey:@"value"]]; 
      } else { 
       [sql appendFormat:@"%@='%@'", 
       [[dbData objectAtIndex:i] objectForKey:@"key"], 
       [[dbData objectAtIndex:i] objectForKey:@"value"]]; 
      }  
      if (i + 1 < [dbData count]) { 
       [sql appendFormat:@", "]; 
      } 
     } 
     if (where != NULL) { 
      [sql appendFormat:@" WHERE %@",where]; 
     } else { 
      [sql appendFormat:@" WHERE 1",where]; 
     }  
     [self runDynamicSQL:sql forTable:table]; 
    } 

    - (void)updateDictionary:(NSDictionary *)dbData forTable:(NSString *)table { 
     [self updateDictionary:dbData forTable:table where:NULL]; 
    } 

    - (void)updateDictionary:(NSDictionary *)dbData forTable:(NSString *)table where:(NSString *)where { 

     NSMutableString *sql = [NSMutableString stringWithCapacity:16]; 
     [sql appendFormat:@"UPDATE %@ SET ",table]; 

     NSArray *dataKeys = [dbData allKeys]; 
     for (int i = 0 ; i < [dataKeys count] ; i++) { 
      if ([[dbData objectForKey:[dataKeys objectAtIndex:i]] intValue]) { 
       [sql appendFormat:@"%@=%@", 
       [dataKeys objectAtIndex:i], 
       [dbData objectForKey:[dataKeys objectAtIndex:i]]]; 
      } else { 
       [sql appendFormat:@"%@='%@'", 
       [dataKeys objectAtIndex:i], 
       [dbData objectForKey:[dataKeys objectAtIndex:i]]]; 
      }  
      if (i + 1 < [dbData count]) { 
       [sql appendFormat:@", "]; 
      } 
     } 
     if (where != NULL) { 
      [sql appendFormat:@" WHERE %@",where]; 
     } 
     [self runDynamicSQL:sql forTable:table]; 
    } 

    - (void)updateSQL:(NSString *)sql forTable:(NSString *)table { 
     [self runDynamicSQL:sql forTable:table]; 
    } 

    - (void)deleteWhere:(NSString *)where forTable:(NSString *)table { 

     NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@", 
         table,where]; 
     [self runDynamicSQL:sql forTable:table]; 
    } 

    // INSERT/UPDATE/DELETE Subroutines 

    - (BOOL)runDynamicSQL:(NSString *)sql forTable:(NSString *)table { 

     int result; 
     //NSAssert1(self.dynamic == 1,[NSString stringWithString:@"Tried to use a dynamic function on a static database"],NULL); 
     sqlite3_stmt *statement; 
     if (statement = [self prepare:sql]) { 
      result = sqlite3_step(statement); 
     }  
     sqlite3_finalize(statement); 
     if (result) { 
      if (self.delegate != NULL && [self.delegate respondsToSelector:@selector(databaseTableWasUpdated:)]) { 
       [delegate databaseTableWasUpdated:table]; 
      } 
      return YES; 
     } else { 
      return NO; 
     } 

    } 

    // requirements for closing things down 

    - (void)dealloc { 
     [self close]; 
     [delegate release]; 
     [super dealloc]; 
    } 

    - (void)close { 

     if (dbh) { 
      sqlite3_close(dbh); 
     } 
    } 

    @end 
1

Polecam używanie Realm do bardziej ogólnych rozwiązań.

0
// Create Database file 

- (void)createEditableCopyOfDatabaseIfNeeded 
{ 
    // First, test for existence. 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSLog(@"%@",documentsDirectory); 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"TestDB.sqlite"]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) return; 

    // The writable database does not exist, so copy the default to the appropriate location. 

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TestDB.sqlite"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 
+0

Mimo że ten kod może odpowiedzieć na pytanie, podanie dodatkowego kontekstu dotyczącego tego, dlaczego i/lub odpowiedź na pytanie znacznie poprawiłoby jego długoterminową wartość. Edytuj odpowiedź, aby dodać wyjaśnienie. – gevorg

1

Masz kilka różnych sposobów na przechowywanie danych aplikacji na telefonie iPhone.

  1. PLIST pliki
  2. NSUserDefaults
  3. UserDefault
  4. File System
  5. CoreData
  6. SQLite DB
Powiązane problemy