我想获得结果集中的行数。我正在使用FMDB进行数据库操作。还有一个函数hasAnotherRow()
但它每次返回true
。以下是我的代码。
let selectQuery = "SELECT * FROM \(tableName) WHERE chrSync = 'Y'"
if let rs = database.executeQuery(selectQuery, withArgumentsIn: [])
{
// Here I want to check if rs has more than 0 rows
while(rs.next()){
let dir = rs.resultDictionary
let json = JSON(dir)
data.append(json)
print("Has another: \(rs.hasAnotherRow())") // It always returns true
}
let json = JSON(data)
return json
}
我是IOS的新手,所以请分享我的链接,如果有答案的话。
谢谢。
我认为这是关键(https://github.com/aws-amplify/aws-sdk-ios/tree/master/AWSCore/FMDB):
在尝试访问查询中返回的值之前,您必须始终调用 - [FMResultSet next],即使您只期望一个:
FMResultSet *s = [db executeQuery:@"SELECT COUNT(*) FROM myTable"];
if ([s next]) {
int totalCount = [s intForColumnIndex:0];
}
迅速
var s: FMResultSet? = db.executeQuery("SELECT COUNT(*) FROM myTable", withArgumentsIn: nil)
if s?.next() != nil {
var totalCount: Int? = s?.int(forColumnIndex: 0)
}
它可以用于任何SELECT,如果select返回行,则totalCount的最后一个值将具有FMResultSet中的行数。
FMResultSet *resultSet = [db executeQuery:@"SELECT COUNT(*) FROM myTable"];
int numRows = 0;
if ([resultSet next]) {
numRows = [resultSet intForColumn:@"COUNT(*)"];
}
上面的代码段将为您提供一个名为“COUNT(*)”的列作为结果。 [resultSet next]将遍历查询结果中的所有行(在本例中为1行)。所以这也可以用来获得行数。