Use of FMDB, use of FMDB
Use of FMDB
I. Three Important FMDB classes
1.1 FMDataBase: Provides SQLite database classes for executing SQL statements.
1.2 FMResultSet: class used to execute query results in FMDatabase
1.3 FMDatabaseQueue: class used to query and update databases under multiple threads
Ii. Use FMDB
2.1 create a database
FMDatabase * db = [FMDatabase databaseWithPath: @ "/tmp. db"];
2.2 Open a database
2.3 database update (except for the select statement, it can be called an update operation)
2.4 Database Query
Execute the select statement by calling-executeQuery.
A FMResultSet object is returned if the result is successful.
Nil is returned if the request fails.
(You can use the-lastErrorMessage and-lastErrorCode methods to confirm why the query fails)
To traverse the query results, you need to loop through while () and view records one by one. In FMDB, the following simple method can be used:
FMResultSet * s = [db executeQuery: @ "SELECT * FROM myTable"];
While ([s next]) {
.....
}
Even if you only need to obtain one data, you must call-[FMResultSet next] before accessing the query results.
FMResultSet * s = [db executeQuery: @ "select count (*) FROM myTable"];
If ([s next]) {
Int totalCount = [s intForColumnIndex: 0];
}
FMResultSet provides many convenient methods to query data:
- IntForColumn:
- LongForColumn:
- LongLongIntForColumn:
- BoolForColumn:
- DoubleForColumn:
- StringForColumn:
- DateForColumn:
- DataForColumn:
- DataNoCopyForColumn:
- UTF8StringForColumn:
- ObjectForColumn:
Generally, there is no need to manually close an FMResultSet because the result set is released or the source database is closed automatically.
2.5 shut down the database
[Db close];