2012-01-27 8 views
1

SQLite3 DBに3つのデータを追加しようとしています。チュートリアルの助けを借りて、私はほとんどそこにいます。以下は私のコードです。XcodeからSQLite3 DBにデータを追加

私はNSLogsから期待していることを受け取ります。私は問題のないDBを見つける、問題なくDBに送信するために文をコンパイルしますが、アプリケーションがsqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OKに到着すると、データがDBに追加されることを期待していますが、そうではありません。ユーザにERRORまたはSUCCESSを知らせるためにUIAlertViewを表示する。私はあなたが私が何をしていないかを見ることができるといいです

- (IBAction)addData:(id)sender { 
    NSString *n = [[NSString alloc] initWithString:[name text]]; 
    NSString *a = [[NSString alloc] initWithString:[age text]]; 
    NSString *c = [[NSString alloc] initWithString:[color text]]; 
    NSLog(@"%@ - %@ - %@",n,a,c); 
    [self insertDataName:n Age:a Color:c]; 
} 

このNSLogは、期待どおりのテキストプロパティを返します。

- (IBAction)hide:(id)sender { 
    [name resignFirstResponder]; 
    [age resignFirstResponder]; 
    [color resignFirstResponder]; 

} 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    DBName = @"mydatabase.sqlite"; 
    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentsPaths objectAtIndex:0]; 
    DBPath = [documentsDir stringByAppendingPathComponent:DBName]; 

} 


-(void)checkAndCreateDB { 
    BOOL Success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    Success = [fileManager fileExistsAtPath:DBPath]; 

    if(Success) { 
     NSLog(@"DB Found");  
     return; 
    } 

    NSLog(@"DB Not Found"); 
    NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName]; 
    [fileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; 

} 

このNSLog戻り

-(void)insertData Name:(NSString*)n Age:(NSString*)a Color:(NSString*)c { 
    [self checkAndCreateDB]; 
    sqlite3 *database; 
    if (sqlite3_open([DBPath UTF8String],&database)==SQLITE_OK) { 
     NSString *statement; 
     sqlite3_stmt *compliedstatement; 
     statement = [[NSString alloc] initWithFormat:@"insert into table1 values ('%@', '%@', '%@')",n,a,c]; 
     const char *sqlstatement = [statement UTF8String]; 
     NSLog(@"%@",statement); 

     if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) { 

     if (SQLITE_DONE!=sqlite3_step(compliedstatement)) { 
      NSAssert1(0, @"Error by inserting '%s'", sqlite3_errmsg(database)); 
      UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error by inserting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  

      [AlertOK show]; 

     } 
     else { 
      UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Data successfully inserted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];    
      [AlertOK show]; 
      } 
     } 
     sqlite3_finalize(compliedstatement); 

    } 
    sqlite3_close(database); 

} 

そして私が期待するよう表示さinsert into table1 values ('n', 'a', 'c')上記の最後のNSLog

挿入時に何かがうまくいかず、わかりません。

FYI SQLiteManagerを使用してDBを作成しました。以下SS:

SQLiteManager table1 SS

+0

を行うポップしたい場合、あなたは正確に何が起こったかのより具体的なことはできますか?あなたは声明が準備されていますが、どのような誤りがありますか?これ(SQLITE_DONE!= sqlite3_step(compliedstatement))が実行されましたか?それで、どうなった? – Byte

+0

私は成功 'UIAlertView'を取得しましたが、SQLiteManagerでデータベースを開き、' select * from table1'を開くと、0個のエントリがあります。 –

答えて

1

[OK]を、あなたはシミュレータやのiDeviceに取り組んでいますか? iDeviceでは、少なくとも私の知る限り、外部SQLデータベースを作成することはできません。サンドボックス化のために動的に作成する必要があります。 「FYI SQLiteManagerを使用してDBを作成しました。それをチェックしてください。

それは問題ではなかった場合からあなたのコードを変更してみてください "準備..." ...

if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) { 

    if (SQLITE_DONE!=sqlite3_step(compliedstatement)) { 
     NSAssert1(0, @"Error by inserting '%s'", sqlite3_errmsg(database)); 
     UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error by inserting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  

     [AlertOK show]; 

    } 
    else { 
     UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Data successfully inserted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];    
     [AlertOK show]; 
     } 
    } 
    sqlite3_finalize(compliedstatement); 

から

を "実行" する...

sqlite3_exec(database, [sqlstatement UTF8String], NULL, NULL, &error); 

これは非常に必須であるため、「エラー」にログオンしてください。

あなたはUIAlertこの

if (error !=nil) 
    //show alert for error 
else 
    //show alert for success 
+0

偉大な答え、これは正しい方向に私を指摘した。ありがとう! –

関連する問題