-1

データベースをフェッチし、クエリを介して更新削除を挿入する必要があるため、どのクラスでも簡単な操作で使用できます

 +(void)createEditableCopyOfDatabaseIfNeeded {
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"YOUR DB NAME"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) {
        return;
    }
    NSString *defualtDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"YOUR DB NAME"];
    success = [fileManager copyItemAtPath:defualtDBPath toPath:writableDBPath error:&error];

    if (!success) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Database Error!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

クエリによる完全な操作が必要です...

4

1 に答える 1

1

以下のファイルを作成し、これをdatabase.hファイルに書き込みます

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

    @interface Database : NSObject {
   }
   +(NSString* )getDatabasePath;
   +(NSMutableArray *)executeQuery:(NSString*)str;
   +(NSString*)encodedString:(const unsigned char *)ch;
   +(BOOL)executeScalarQuery:(NSString*)str;
   @end

これはdatabase.mファイルになります

   #import "Database.h"
 #import <sqlite3.h>


  @implementation Database

  +(NSString* )getDatabasePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"YOURDBNAME"];
return writableDBPath;

    }

  +(NSMutableArray *)executeQuery:(NSString*)str
  {
sqlite3_stmt *statement= nil;
sqlite3 *database;
NSString *strPath = [self getDatabasePath];
NSMutableArray *allDataArray = [[NSMutableArray alloc] init];
if (sqlite3_open([strPath UTF8String],&database) == SQLITE_OK) 
{
    if (sqlite3_prepare_v2(database, [str UTF8String], -1, &statement, NULL) == SQLITE_OK) 
    {
        while (sqlite3_step(statement) == SQLITE_ROW) 
        {
            NSInteger i = 0;
            NSInteger iColumnCount = sqlite3_column_count(statement);
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            while (i< iColumnCount) {
                NSString *str = [self encodedString:(const unsigned char*)sqlite3_column_text(statement, i)];
                NSString *strFieldName = [self encodedString:(const unsigned char*)sqlite3_column_name(statement, i)];
                [dict setObject:str forKey:strFieldName];
                i++;
            }

            [allDataArray addObject:dict];
            [dict release];
        }
    }
    else {
         NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
    }

    sqlite3_finalize(statement);
} 
sqlite3_close(database);
return allDataArray;
  }
  +(BOOL)executeScalarQuery:(NSString*)str{

sqlite3_stmt *statement= nil;
sqlite3 *database;
BOOL fRet = NO;
NSString *strPath = [self getDatabasePath];
if (sqlite3_open([strPath UTF8String],&database) == SQLITE_OK) {
    if (sqlite3_prepare_v2(database, [str UTF8String], -1, &statement, NULL) == SQLITE_OK) {
        if (sqlite3_step(statement) == SQLITE_DONE)
            fRet =YES;
    }

    sqlite3_finalize(statement);
} 
sqlite3_close(database);
return fRet;
   }


   +(NSString*)encodedString:(const unsigned char *)ch
    {
NSString *retStr;
if(ch == nil)
    retStr = @"";
else
    retStr = [NSString stringWithCString:(char*)ch encoding:NSUTF8StringEncoding];
return retStr;
   }

   @end
于 2013-09-23T06:44:20.207 に答える