FMDB Swift SQL Get Number of Rows in Table

3.2k views Asked by At

FMDB seems to have lots of functions to deal with columns but none for rows. All I am trying to do is find the number of rows in a table.

func getTableRows() {
    sharedInstance.database!.open()
    let sqlStatement = "SELECT COUNT(*) FROM NAMESTABLE"
    var resultSet: FMResultSet! = sharedInstance.database!.executeQuery(sqlStatement, withArgumentsInArray: nil)
    if (resultSet != nil) {
        let rowCount = XXXX
        NSLog("Table Rows = %i",rowCount)
    }
    sharedInstance.database!.close()
}

I have tried various approaches with the XXXX but no joy. Am I going about this the wrong way? Many thanks.

1

There are 1 answers

4
Özgür Ersil On BEST ANSWER

try this:

func getTableRows() {
    sharedInstance.database!.open()
    let sqlStatement = "SELECT COUNT(field) FROM table_name"
    var resultSet: FMResultSet! = sharedInstance.database!.executeQuery(sqlStatement, withArgumentsInArray: nil)
    if (resultSet != nil) {
        let rowCount = XXXX
        NSLog("Table Rows = %i",rowCount)
    }
    sharedInstance.database!.close()
}