how to store NSString to Database as INTEGER?
本问题已经有最佳答案,请猛点这里访问。
如何将
当更新数据库时,应用程序得到异常终止。
①用户通过
我有这个密码。
书籍类操作数据库。
书,M
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #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; } |
编辑视图控制器.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | -(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]; } } |
表视图控制器.m
1 2 3 4 5 6 7 8 9 10 11 12 13 | - (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; } |
我知道怎么修吗?事先谢谢。
1 | [NSString stringWithFormat:@"%d", count]; |
试试这个
1 2 3 4 5 6 7 8 9 10 11 12 13 | - (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; } |