2011-02-03 16 views
3

sqlite3 * database; sqlite3_stmt *ステートメント。NSString * dPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@ "UserData.sqlite"]; const char * dbpath = [dPath UTF8String]; // NSLog(@ "%@"、dbpath); // UserArray = [[NSMutableArray alloc] init];テーブルにデータを挿入する際の問題

if(sqlite3_open(dbpath, &database) == SQLITE_OK) 
{ 

    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO User (UserName,FullName,Email,PhoneNo) VALUES (\"%@\", \"%@\", \"%@\" ,\"%@\")",txtUserName.text,txtFullName.text ,txtEmail.text , txtPhoneNo.text]; 

    const char *insert_stmt = [insertSQL UTF8String]; 

    sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL); 

    if(sqlite3_step(statement)==SQLITE_DONE) 
    { 
     [email protected]""; 
     [email protected]""; 
     [email protected]""; 
     [email protected]""; 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Record" message:@"Contact Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  
     [alert show]; 
     [alert release]; 
     alert=nil; 

    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record not created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
     alert=nil; 
    } 


    // Release the compiled statement from memory 
    sqlite3_finalize(statement); 

    sqlite3_close(database); 
} 

- >>テーブルにデータを挿入しようとしています。コードは正しく実行されますが、レコードはテーブルに追加されません。だから、私の手助けをしてください。

答えて

0

ので、私は、なぜあなたはあなたのアプリケーションバンドルであるデシベルのINSERT操作を実行しているかと思います

// COMMIT 
    const char *sql2 = "COMMIT TRANSACTION"; 
    sqlite3_stmt *commit_statement; 
    if (sqlite3_prepare_v2(database, sql2, -1, &commit_statement, NULL) != SQLITE_OK) { 
    NSAssert1(0, @"Error Message: '%s'.", sqlite3_errmsg(database)); 
    } 
    success = sqlite3_step(commit_statement); 
    if (success != SQLITE_DONE) { 
    NSAssert1(0, @"Error Message: '%s'.", sqlite3_errmsg(database)); 
    } 
} 
+0

このコードでコミットを実行する方法。 – Sam007

+0

答えを改訂しました –

0

を自動コミットするデシベルを設定し、コミットを実行してみてください。このサンプルコードを試してみてください。

-(void)checkAndCreateDB { 
    NSString* databaseName = @"MasterDB.sqlite"; 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 
    BOOL success1; 
    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]; 
} 

-(void)insertData{ 

    sqlite3 *database; 
    sqlite3_stmt *statement; 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
    { 

     NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO User (uId,userName) VALUES (\"%d\", \"%@\")",1,@"RAHUL"]; 
     const char *insert_stmt = [insertSQL UTF8String]; 
     sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL); 

     if(sqlite3_step(statement)==SQLITE_DONE) 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Record" message:@"Contact Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  
      [alert show]; 
      [alert release]; 
      alert=nil; 

     } 
     else 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record not created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
      alert=nil; 
     } 
     // Release the compiled statement from memory 
     sqlite3_finalize(statement);  
     sqlite3_close(database); 
    } 
} 

ここで私のプロジェクトリソースフォルダにMasterDB.sqliteファイルがコピーされています。アプリケーションを実行している間は、通常、アプリケーション文書フォルダにDBをコピーしてから、READ/WRITE操作を実行します。そのINSERTオペレーションを実行する最低限のコードは、CheckAndCreatreDBの後にinsertData関数を呼び出すことだけです。変数databasePathは、これらのメソッドを含むクラスの.hファイルで宣言されています。

これが役立ちますように!

関連する問題