Ios FMDB class library usage learning notes
This article is excerpted from: apsaradb for iOS development-FMDB
What is FMDB?
FMDB is the sqlite database framework of the ios platform.
FMBD encapsulates the C language API of sqlite in oc Mode
Advantages of FMDB
It is more object-oriented and saves a lot of trouble and redundant C language code.
More lightweight and flexible than Apple's core Data framework
Provides a multi-threaded and secure database operation method to effectively prevent data confusion.
FMDB github address
Www.bkjia.com
Core
FMDB has three main classes
1. FMDatabase
An FMDatabase object represents a separate sqlite database to execute SQL statements.
2. FMResultSet
Result set after the query is executed using FMDatabase
3. FMDatabaseQueue
It is used to execute multiple queries or updates in multiple threads. It is thread-safe.
Open Database
Create an FMDatabase object by creating an sqlite database file path
FMDatabase * db = [FMDatabase databaseWithPath: path]; if (! [Db open]) {NSLog (@ database open failed !);}
There are three file paths:
1. Specific file path (automatically created if the file does not exist)
2. Empty string @ (an empty database will be created in the temporary directory. When the FMDatabase connection is closed, the database file will also be deleted)
3. nil (create a temporary database in memory. When the FMDatabase connection is closed, the database will be destroyed)
Execute update
In FMDB, all operations except query exceptions are called updates.
Create, drop, insert, update, delete, etc.
Execute updates using the executeUpdate Method
- (BOOL)executeUpdate:(NSString*)sql, ...- (BOOL)executeUpdateWithFormat:(NSString*)format, ...- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments
Example
[db executeUpdate:@UPDATE t_student SET age = ? WHERE name = ?;, @20, @Jack]
Query Method execution
- (FMResultSet *)executeQuery:(NSString*)sql, ...- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ...- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments
Example
// Query data FMResultSet * rs = [db executeQuery: @ SELECT * FROM t_student];
while ([rs next]) { NSString *name = [rs stringForColumn:@name]; int age = [rs intForColumn:@age]; double score = [rs doubleForColumn:@score];}
Use of class libraries:
// Open the database + (FMDatabase *) openDB {NSString * filePath = [using (NSDocumentationDirectory, NSUserDomainMask, YES) lastObject]; NSString * fileName = [filePath stringByAppendingString: @ BaiduMap. sqlite]; FMDatabase * db = [FMDatabase databaseWithPath: fileName]; if ([db open]) {return db;} return nil ;} # pragma mark-database upDate // all operations except query are upDate + (BOOL) upDate: (NSString *) SQL {FMDatabas E * db = [self openDB]; if (db! = Nil) {return [db executeUpdate: SQL];} return NO;} # pragma mark-database creation table + (BOOL) createTable {FMDatabase * db = [self openDB]; if (db! = Nil) {NSString * SQL = @ CREATE TABLE location_record (ID INTEGER PRIMARY KEY AUTOINCREMENT, latitude REAL NOT NULL, long1_real NOT NULL, time TEXT NOT NULL; return [db executeUpdate: SQL];} return NO;} # pragma mark-insert data + (BOOL) insert :( float) latitude :( float) longpolling :( NSString *) time {NSString * SQL = [NSString stringWithFormat: @ INSERT INTO location_record (latitude, longpolling, time) values (% f, % f, % @), Latitude, longpolling, time]; return [self upDate: SQL] ;}# pragma mark-query + (NSArray *) query :( NSString *) starTime :( NSString *) endTime :( int) startNumber {FMDatabase * db = [self openDB]; if (db! = Nil) {NSString * SQL = [NSString stringWithFormat: @ SELECT (latitude, longpolling) FROM location_record WHERE time >%@ AND time <% @ limit % d, 60, starTime, endTime, startNumber]; FMResultSet * resultSet = [db executeQuery: SQL]; NSMutableArray * array = [NSMutableArray array]; while ([resultSet next]) {double latitude = [resultSet doubleForColumnIndex: 1]; double longpolling = [resultSet doubleForColumnIndex: 2]; [array addObject: [NSNumber numberWithDouble: latitude]; [array addObject: [NSNumber numberWithDouble: longpolling];} return array;} return nil ;}
People planted trees and enjoying the coolness -- never forget to dig for water
·