English 中文(简体)
FMDB SQLite question: row count of a query?
原标题:

does anyone know how to return the count of a query when using FMDB? If I executeQuery @"select count(*) from sometable were..." I get an empty FMResultSet back. How can I get the row count of the query? Do I need to do a query like "select * from sometable where.." and iterate through the result set? Or can I use useCount or whats the best way (in terms of performance) to do this?

Thanks!

最佳回答

Shorter code to accomplish the same thing:

NSUInteger count = [db intForQuery:@"SELECT COUNT(field) FROM table_name"];

Make sure to include the FMDatabaseAdditions.h header file to use intForQuery:.

问题回答

try this. It works for me. Iterating all the records is not recommended.

FMResultSet *rs = [db executeQuery:@"select count(FIELD) as cnt from TABLENAME"];
while ([rs next]) {
    NSLog(@"Total Records :%d", [rs intForColumn:@"cnt"]);
}

May be you should check your Where clause.

Swift 2 Example

This code snippet will print the count for you.

if let rs = db.executeQuery("SELECT COUNT(*) as Count FROM TABLE_NAME", withArgumentsInArray: nil) {
    while rs.next() {
        print("Total Records:", rs.intForColumn("Count"))
    }
}

If it did not work, a few suggestions:

a) Look for a line in your project that says let database = or var database =. If you find one then change db to database
b) Did you change the TABLE_NAME in the Select statement to whatever your table is called?

The first one is also right but by using this method you can retrieve records and count using the same query , no headache to write another one. Just add count(*) as count to your query.

You could always just run the proper SQL statement. I do something like:

FMResultSet *rs = [database executeQuery:@"select count(*) as count from words"];
[rs next];

wordsThatExist = [rs intForColumn:@"count"];

Setting up the SQL query may be quicker and cheaper then iterating.. I believe counts are cheap.

updated for Swift 3 minor change to "int For Column"

if let rs = db.executeQuery("SELECT COUNT(*) as Count FROM TABLE_NAME", withArgumentsInArray: nil) {
while rs.next() {
    print("Total Records:", rs.int(forColumn: "Count"))
    }
}

updated for Swift 4 minor change in method parameter name

if let rs = db.executeQuery("SELECT COUNT(*) as Count FROM TABLE_NAME", withArgumentsIn: nil) {
while rs.next() {
    print("Total Records:", rs.int(forColumn: "Count"))
    }
}

Please Try Following Code, this works for me

let objManager = ModelManager.getInstance()

objManager.database?.open()

let resultSet1: FMResultSet! = sharedInstance.database!.executeQuery("SELECT COUNT(Field) FROM TableName”, withArgumentsInArray:nil)

        if (resultSet1 != nil)
        {
            while resultSet1.next()
            {
              countRecord = Int(resultSet1.intForColumn("COUNT(Field)"))
            }

        }

     print(countRecord)

You Will get Count of Field





相关问题
sqlite3 is chopping/cutting/truncating my text columns

I have values being cut off and would like to display the full values. Sqlite3 -column -header locations.dbs " select n.namelist, f.state, t.state from names n left join locations l on l.id = n.id ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Improve INSERT-per-second performance of SQLite

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop ...

Metadata for columns in SQLite v2.8 (PHP5)

How can I get metadata / constraints (primary key and "null allowed" in particular) for each column in a SQLite v2.8 table using PHP5 (like mysql_fetch_field for MySql)? sqlite_fetch_column_types (OO:...

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签