【问题标题】:How to get integer value from Sqlite Database?如何从 Sqlite 数据库中获取整数值?
【发布时间】:2012-04-27 19:26:06
【问题描述】:

我正在使用 Sqlite 数据库。

在我的数据库中

我已经提交了一些喜欢、发票、姓名、日期

我的 Invoiceno 数据类型是 INTEGER,

在选择查询中得到了这样的输出。

 Query:(
    {
     InvoiceNo = 1;
     Name : ABC
     Date = "2012-04-16 00:00:00";

    }
  )

但是当尝试获取那个 Invoice no 字符串时,它给出了 Diff。价值。

从数组获取发票到字符串的代码:

  NSString *str = [NSString stringWithFormat:@"%i",[[Query objectAtIndex:indexPath.row] valueForKey:@"InvoiceNo"]];
    NSLog(@"str:%@",str);

这里,我的发票编号是 1,它的值是“2387056”

请帮帮我..我该如何解决这个问题?

【问题讨论】:

  • [NSString stringWithFormat:@"%d",[[Query objectAtIndex:indexPath.row] valueForKey:@"InvoiceNo"]];也许它会帮助你。
  • %d 和 %i 的结果应该是 32 位整数
  • @chinttu 感谢您的回答,它给了我其他字符串值...
  • @Anki 你必须做 %d 或做 typecast "intValue" .. 也许它会有所帮助。你有答案吗?

标签: iphone objective-c cocoa-touch ios4 sqlite


【解决方案1】:
NSString *str = [NSString stringWithFormat:@"%i",[[Query objectAtIndex:indexPath.row] valueForKey:@"InvoiceNo"]];
    NSLog(@"str:%@",str);
you have to replace above lines with below lines

NSString *str = [NSString stringWithFormat:@"%i",[[[Query objectAtIndex:indexPath.row] valueForKey:@"InvoiceNo"] intValue]];
    NSLog(@"str:%@",str);

【讨论】:

    【解决方案2】:

    Convert NSString to int

    NSString *values = @"1";
    
    int yourValue = [values intValue];
    
    NSLog(@"Int Value : %d",yourValue);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-28
      • 2018-01-30
      • 2019-05-21
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多