2014-01-07 31 views
-3

如何將NSString改爲Database改爲INTEGER如何將NSString作爲INTEGER存儲到數據庫?

更新數據庫時,應用程序獲取Abend。

①User通過方法intValue輸入一個數字UITextField * numTextField通過UIPickerView[1~10]
②convertnumTextField.text轉換成int num_int。
③將數據存儲到數據庫
④通過方法stringValue將book.num轉換爲NSString * num_text。
⑤outputnum_text到numTextFieldcell.label3.text

我有這樣的代碼。

書類操作數據庫。

Book.m

#define SQL_INSERT @"INSERT INTO books1 (day, title, time, num) VALUES (?, ?, ?, ?);" 

- (Book*)add:(Book *)book{ 
FMDatabase* db = [self getConnection]; 
[db open]; 
[db setShouldCacheStatements:YES]; 

if([db executeUpdate:SQL_INSERT, book.day, book.title, book.time, book.num] { 
    book.bookId = [db lastInsertRowId]; 
    } 

else { 
    book = nil; 
} 

[db close]; 

return book; 
} 

EditViewController.m

-(void)viewDidLoad{ 
if(self.book) 
    { 
     _titleTextField.text = self.book.title; 
     _dayTextField.text = self.book.day; 
     _timeTextField.text = self.book.time; 

     int num_int = book.num; 
     NSNumber *num_n = [[NSNumber alloc] initWithInt:num_int]; 
     NSString *num_text = [num_n stringValue]; 
     cell.label3.text = num_text; 
    } 
} 

- (IBAction)saveData:(id)sender 
{  
    Book* newBook = [[Book alloc] init]; 
    newBook.bookId = self.book.bookId; 
    newBook.title  = _titleTextField.text; 
    newBook.day = _dayTextField.text; 
    newBook.time = _timeTextField.text; 

    NSString *num_text = _numTextField.text; 
    int num_int = [num_text intValue]; 
    newBook.num = num_int; 

if(self.book){ 
    [self.delegate editBookDidFinish:self.book newBook:newBook]; 
} 
else{ 
    [self.delegate addBookDidFinish:newBook]; 
} 
} 

TableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Book* book = [self bookAtIndexPath:indexPath]; 
    cell.label1.text = book.title; 
    cell.label2.text = book.time; 

    int num_int = book.num; 
    NSNumber *num_n = [[NSNumber alloc] initWithInt:num_int]; 
    NSString *num_text = [num_n stringValue]; 
    cell.label3.text = num_text; 

    return cell; 
} 

在任何輸入到numTextField.textcell.label3.text的輸出「0」。
我想將num_text作爲INTEGER存儲到數據庫中,因爲num將被添加並被按鈕減去。

關於如何修復它的任何想法? 在此先感謝。

+2

看看http://stackoverflow.com/questions/1448804/how-to-convert-an-nsstring-into-an-nsnumber/。請在下次問你的問題之前嘗試快速搜索! – Eric

回答

0

試試這個

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     Book* book = [self bookAtIndexPath:indexPath]; 
     cell.label1.text = book.title; 
     cell.label2.text = book.time; 

     int num_int = book.num; 
     NSNumber *num_n = [[NSNumber alloc] initWithInt:num_int]; 
     NSString *num_text = [NSString stringWithFormate:@"%@",num_n] 
     cell.label3.text = num_text; 

    return cell; 
} 
+0

謝謝。我試過這個。但是當應用程序「 - (IBAction)saveData」 –

0
[NSString stringWithFormat:@"%d", count]; 
相關問題