2010-11-29 15 views

答えて

3

あなたは、あなたがそれと対話することができ、これらの2つの方法

-(id) initDatabase{ 
    databaseName = @"db.sqlite"; 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 
    [databasePath retain]; 

    return self; 
} 

-(void) checkAndCreateDatabase{ 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    success = [fileManager fileExistsAtPath:databasePath]; 
    if(success) return; 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 
    [fileManager release]; 
} 

でDBを初期化する必要があります:まず

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
    static sqlite3_stmt *compiledStatement; 
    sqlite3_exec(database, [[NSString stringWithFormat:@"insert into myTable (var1, var2) values ('%@', '%@')", myVar1, myVar2] UTF8String], NULL, NULL, NULL); 
    sqlite3_finalize(compiledStatement); 
} 
sqlite3_close(database); 
+0

THANKあなたは恒久的な答えを求めています!しかし、fisrtと2番目のメソッドを実装すると、2つのエラーが発生します。 "databaseName undeclared"と "databasepath undeclared" –

+0

NSString * databaseName; NSString * databasePath; sqlite3 *データベース; char * sqlStatement; sqlite3_stmt * compiledStatement; ...それは動作するはずです! – vincent

+0

申し訳ありませんが、私は真の初心者です...どこでこれらの変数を宣言しなければなりませんか? –

0

それが簡単にDB

にデータを格納するために
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSString *dbPath =[documentsDir stringByAppendingPathComponent:@"register.sqlite"]; 
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 
    sqlite3_stmt *selectstmt; 
    if(!success) 
    { 
     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"register.sqlite"]; 
     success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 

     if (!success) 
      NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { 
     //*************** insert value in database******************************\\ 

     const char *sql = "insert into reg_FORM (firstname,lastname,email,company,phone) VALUES ('nannav','cs','[email protected]','ibm',123456789)"; 
     sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL); 
     if(sqlite3_step(selectstmt)==SQLITE_DONE) 
     { 
      NSLog(@"insert successfully"); 
     } 
     else 
     { 
      NSLog(@"insert not successfully"); 

     } 
    sqlite3_finalize(selectstmt); 
     sqlite3_close(database); 
    } 
} 
関連する問題