2011-05-20 137 views
0

HII每一個無法在OBJ C到將數據插入SQLite數據庫

我使用下面的方法將數據插入到數據庫中,而且可以節省第一個輸入的值只有所有的時間 下面的方法是所述insertUpdateDelete類

- (void) InsertRecord { 


    if(addStmt == nil) { 

     NSString *nsql = [NSString stringWithFormat:@"insert into tbl_Users(FirstName,MiddleName) Values('%@','%@')",strFirstName,strMiddleName]; 
     const char *sql = [nsql UTF8String]; 



     if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK) 
     { 
      NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database)); 

     } 
    } 



    if(SQLITE_DONE != sqlite3_step(addStmt)) 
     NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database)); 
    else 
     //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid 
     intID = sqlite3_last_insert_rowid(database); 

    //Reset the add statement. 
    sqlite3_reset(addStmt); 
} 

通過下面的代碼我正在調用該方法,tfText [0] & tfText [1]是文本字段變量,問題是,,在進入在文本字段中的一些數據後保存的每一次點擊它只會將第一個輸入值保存到數據庫中

- (void) save_Clicked 
    { 


     iICS_testAppDelegate *appDelegate = (iICS_testAppDelegate *)[[UIApplication sharedApplication] delegate]; 

     //Create a Items Object. 
     insertUpdateDelete *objInsertUpdateDelete = [[insertUpdateDelete alloc] init]; 

     objInsertUpdateDelete.strFirstName = tfText[0].text; 
     objInsertUpdateDelete.strMiddleName = tfText[1].text; 

     [appDelegate InsertRecord:objInsertUpdateDelete]; 

    } 

任何一個可以幫助我,,,, thanx提前

回答

2
const char *addRecord = "insert into Test(taskname, desc) values(?, ?)"; 
sqlite3_stmt *statement; 

if(sqlite3_prepare_v2(database, addRecord, -1, &statement, NULL) != SQLITE_OK) 
{ 
    NSLog(@"Error while Inserting Record :- '%s'", sqlite3_errmsg(database)); 
    sqlite3_finalize(statement); 
    return -1; 
} 

sqlite3_bind_text(statement, 1, [Ttitle UTF8String], -1, SQLITE_TRANSIENT); 

sqlite3_bind_text(statement, 3, [Tdesc UTF8String], -1, SQLITE_TRANSIENT); 

if(SQLITE_DONE != sqlite3_step(statement)) 
{ 
    NSLog(@"Error1 while Inserting Record :- '%s'", sqlite3_errmsg(database)); 
    sqlite3_finalize(statement); 
    return -1; 
} 
else 
{ 
    NSLog(@"Record Inserted Successfully."); 
    sqlite3_finalize(statement); 
    return sqlite3_last_insert_rowid(database); 
} 
+0

非常感謝,它工作得很好,,,我的代碼也是正確的認識Ÿ它沒有更新的數據? – Ravi 2011-05-20 08:50:23

+0

只需檢查變量的值並調試代碼...您將堅定地獲得解決方案。 – SJS 2011-05-20 08:52:26

+0

好的,試試,謝謝你 – Ravi 2011-05-20 09:12:44

相關問題