【问题标题】:Fetching data from SQLite and want to get only the last value of column id从 SQLite 获取数据并且只想获取列 id 的最后一个值
【发布时间】:2014-11-18 16:39:55
【问题描述】:

我正在从 SQLite 获取数据,并且只想获取 XCode 中列 id 的最后一个值。代码是

NSString *selquery = @"select id from watchlists";
if (self.uid != nil) {
    self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSString *valvar;

valvar = [_uid lastObject];

NSNumber *custval = [_uid valueForKey: @"@lastObject"];
NSString *imgval1 = [NSString stringWithFormat:@"%@_%s",custval,"1"];
NSLog(@"%@", imgval1);

请告诉我如何才能只获取值,因为通过使用上面的代码,我得到了最后一个 id 值的数组。

【问题讨论】:

  • 你使用什么库:dbManager
  • 好吧,通常你会在 SQL 查询中将 fetch 结果限制为 1:stackoverflow.com/questions/6329032/…
  • 如何获得一个值,它给出了数组中的最后一个值,我不希望它出现在数组中

标签: ios objective-c sqlite


【解决方案1】:

我认为这是你的情况,试试这个也许对你有帮助

NSArray *temp=[NSArray arrayWithObjects:@"1",@"2",@"3", nil];
    NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
   // NSString *tmmp=[temp0ne lastObject];
    NSArray *finalStr=[uid lastObject];
    NSLog(@"Dictionary is---->%@",[finalStr lastObject]);

输出:

3_1

编辑

NSArray *temp=[NSArray arrayWithObjects:@"(1)",@"(2)",@"(3)", nil];
    NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
    NSString *tmmp=[temp0ne lastObject];
    NSString *final=[tmmp stringByReplacingOccurrencesOfString:@"(" withString:@""];
    final=[final stringByReplacingOccurrencesOfString:@")" withString:@""];
    NSString *imgval1 = [NSString stringWithFormat:@"%@_%s",final,"1"];
    NSLog(@"%@", imgval1);

我不知道这是正确的方法还是不尝试这个....否则看看这个link

【讨论】:

  • 我得到的数组是通过从数据库中获取数据((1)、(2)、(3)、(4)、(5)、(6))
  • 好的..将您的数据库代码与第二个数组一起使用..我已经为您的情况做了简单的处理
  • 你如何设置你的 id 列自动增量啊?告诉我们获取代码?
  • NSString *selquery = @"select id from watchlists"; if (self.uid != nil) { self.uid = nil; } self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];这里 UID 是 NsARRAy
  • 我在问数据库代码?这个 self.dbManager 是什么?数组中有数组...
【解决方案2】:

我不完全理解你的代码结构嘿嘿。试试这个:

NSString *selquery = @"select id from watchlists";
if (self.uid != nil) {
    self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];

NSNumber *custval = [_uid objectAtIndex:[_uid count]-1];

*

NSString *str = [NSString stringWithFormat@"%@",custval];

str = [str stringByReplacingOccurrencesOfString:@"("
                                 withString:@""];
NSString *finalCustval = [NSString stringWithFormat@"%@",str];

finalCustval = [finalCustval stringByReplacingOccurrencesOfString:@")"
                                 withString:@""];

*

NSString *imgval1 = [NSString stringWithFormat:@"%@_%s",finalCustval ,"1"];
NSLog(@"%@", imgval1);

更新

尝试添加带 * 的。

【讨论】:

  • 输出是 (6)_1 我只想要 6_1 not()
  • 我已经编辑了我的答案。请检查。对不起,我不记得简短的方法。呵呵
  • 用 "" 替换 ( 的东西是无效的,因为我们想将它与数字连接,但输出显示换行符并且连接在下一行完成。如果你可以连接一个 nuber然后它会帮助我。顺便感谢您的兴趣。
  • 做另一个字符串替换。 imgval1 = [imgval1 stringByReplacingOccurrencesOfString:@"\n" withString:@""];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多