2011-05-10 29 views
2

我是新手...我需要inser語句的幫助..我的代碼如下..代碼運行但它不會在數據庫中添加任何內容。 PLZ任何人都可以建議我爲什麼會這樣..需要幫助,在數據庫中插入

- (IBAction)add:(id)Sender{ 

    CGPoint loc; 
    loc.x = [_x.text floatValue]; 
    loc.y = [_y.text floatValue]; 
    if(loc.x != 0 || loc.y != 0) 
    { 
    [_x endEditing:YES]; 
    [_y endEditing:YES]; 
    [_name endEditing:YES]; 
     NSString *date = [[NSDate alloc]init]; 
     NSNumber *x = [NSNumber numberWithFloat:loc.x]; 
     NSNumber *y = [NSNumber numberWithFloat:loc.y];    

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsPath = [paths objectAtIndex:0]; 
     NSString *filePath = [documentsPath stringByAppendingPathComponent:@"cities.sqlite"]; 

     sqlite3 *database; 

     if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) { 
      const char *sqlStatement = "insert into table (name, xLoc,yLoc,date) VALUES (?, ?, ?, ?)"; 
      sqlite3_stmt *compiledStatement; 
      if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 
       sqlite3_bind_text(compiledStatement, 1, [_name.text UTF8String], -1, SQLITE_TRANSIENT); 

       sqlite3_bind_double(compiledStatement, 2, [x doubleValue]); 

       sqlite3_bind_double(compiledStatement, 3, [y doubleValue]); 

       sqlite3_bind_text(compiledStatement, 4, [date UTF8String] ,-1, SQLITE_TRANSIENT); 

      } 

     char* errmsg; 
     sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg); 

      if(sqlite3_step(compiledStatement) != SQLITE_DONE) { 
       NSLog(@"Error: %s", sqlite3_errmsg(database)); 
      } else { 
       NSLog(@"Insert into row id = %lld", sqlite3_last_insert_rowid(database)); 
      } 
      sqlite3_finalize(compiledStatement); 
     } 
     sqlite3_close(database); 




     [[self navigationController]popViewControllerAnimated:YES]; 
    } 
    else{ 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Wrong Co-ordinates described." delegate:nil cancelButtonTitle:@"Go Back" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 

    } 
} 

感謝guyz我知道了......編輯上面的代碼,現在它運行在數據庫提交數據..

+0

如果它運行...沒有錯誤...但數據庫中沒有任何內容...你做了一個提交? – forsvarir 2011-05-10 12:39:26

+0

是的......它只是它不更新數據庫..是代碼正確..? btw檢查日期語法..如果我想添加日期也到我的表將上面的代碼..謝謝.. – Hadi 2011-05-10 12:41:00

+0

抱歉,但我沒有得到它上次...可以üPLZ編輯我的代碼..如何提交結束.. ima新和它的我的第一次與分貝... – Hadi 2011-05-10 12:52:31

回答

0

你值綁定到了準備好的聲明,但它看起來不像你曾經調用過提交。

+0

哦,sry沒有得到它.. plz編輯我的代碼..在哪裏哈貝我得到致電提交...不步驟做同一件事... sry ma newbie .. – Hadi 2011-05-10 12:46:24