【问题标题】:how to store NSString to Database as INTEGER? [duplicate]如何将 NSString 作为 INTEGER 存储到数据库中? [复制]
【发布时间】:2014-01-24 22:35:21
【问题描述】:

如何将NSString 存储到DatabaseINTEGER

更新数据库时,应用程序异常结束。

①用户在UITextField *numTextField by UIPickerView[1~10]中输入一个数字
②通过intValue方法将numTextField.text转换为int num_int。
③将num存储到DB
④通过stringValue方法将book.num转换为NSString *num_text。
⑤将num_text输出到numTextFieldcell.label3.text

我有这个代码。

Book类操作DB。

书.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;
}

cell.label3.text 在向numTextField.text 输入任何内容时输出“0”。
我想将 num_text 作为 INTEGER 存储到数据库中,因为 num 将通过按钮进行加减。

知道如何解决它吗? 提前致谢。

【问题讨论】:

标签: ios iphone objective-c sqlite fmdb


【解决方案1】:

试试这个

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

【讨论】:

  • 谢谢。我试过了。但是当“-(IBAction)saveData”时应用程序异常结束
【解决方案2】:
[NSString stringWithFormat:@"%d", count];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 2011-10-15
    • 1970-01-01
    • 2015-09-25
    • 2019-08-13
    相关资源
    最近更新 更多