2014-01-07 2 views
-3

NSString ~ Database을 저장하는 방법은 INTEGER입니다.데이터베이스에 NSString을 INTEGER로 저장하는 방법은 무엇입니까?

데이터베이스를 업데이트하면 앱이 이상 종료됩니다.

①User 메소드 intValue 의해 INT의 num_int에 numTextField.text ②convert UIPickerView[1~10]
의해 UITextField * numTextField에 숫자를 입력한다.
DB에 저장 번호
④* num_text 방법으로 stringValue으로 book.num을 변환하십시오.
⑤output의 numTextField에 num_text 및 cell.label3.text

나는이 코드를 가지고있다.

예약 클래스 운영 DB.

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 numTextField.text 아무것도 입력에

- (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; 
} 

cell.label3.text "0"을 출력한다.
num이 num에 추가되어 버튼에서 빼기 때문에 num_text를 INTEGER로 데이터베이스에 저장하고 싶습니다.

어떻게 해결할 수 있습니까? 미리 감사드립니다.

+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]; 
관련 문제