2011-02-03 113 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

嘗試執行提交,所以設置分貝到自動提交

// 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 2011-02-03 20:12:27

0

恐怕您爲何要進行的分貝INSERT操作是在你的應用程序包。嘗試使用此示例代碼我確信可能對你有用

-(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文件。在運行應用程序時,我們通常將數據庫複製到應用程序文檔文件夾中,然後執行任何讀/寫操作。它的一個執行INSERT操作的最小代碼只需調用CheckAndCreatreDB後跟insertData函數。變量databasePath在包含這些方法的類的.h文件中聲明。

希望這有助於!