FMDB for iOS third parties
FMDB has three main classes
1. FMDatabase-indicates a separate SQLite database. The command used to execute SQLite.
2. FMResultSet-indicates the result set after the FMDatabase executes the query.
3. FMDatabaseQueue-if you want to execute multiple queries or updates in multiple threads, you should use this class. This is thread-safe.
Database creation
When creating an FMDatabase object, the parameter is the path of the SQLite database file. The path can be one of the following three types:
1. file path. This file path does not need to be saved. If it does not exist, it is automatically created.
2. Empty string (@""). Creates an empty database in the temporary directory. When the FMDatabase link is closed, the file is also deleted.
3. NULL. An internal database will be created. Similarly, when the FMDatabase connection is closed, the data will be destroyed.
To learn more about the temporary database or internal database, please continue reading: Click to open the link
Open Database
Before interacting with a database, the database must be opened. If resources or permissions are insufficient to open or create a database
- <Pre name = "code" class = "objc"> if (! [_ Db open]) {
- NSLog (@ "database opening failed ");
- Return NO;
- }
Execute update
All commands that are not the SELECT command are regarded as updates. This includes CREATE, UPDATE, INSERT, ALTER, COMMIT, BEGIN, DETACH, DELETE, DROP, END, EXPLAIN, VACUUM, and REPLACE ).
Simply put, the UPDATE command is used as long as the command not starting with SELECT.
Execute the update and return a BOOL value. YES indicates that the execution is successful; otherwise, it indicates that there are errors. You can call the-lastErrorMessage and-lastErrorCode methods to obtain more information.
For example, insert a Student data
- // Two SQL statement writing methods
-
- NSString * sqlStr = [NSString stringWithFormat: @ "insert into student values ('% @', '% @', '% @')", student. number, student. name, student. age];
- NSLog (@ "% @", sqlStr );
- BOOL result = [_ db executeUpdate: sqlStr]; <pre name = "code" class = "objc" style = "color: rgb (51, 51, 51 ); font-size: 14px; line-height: 25px; "> // BOOL result = [_ db executeUpdate: @" insert into student (number, name, age) values (?,?,?) ", Student. number, student. name, student. age];
-
- [Self judgeDBError: result action: DATABASE_INSERT]; return result;
-
-
- /**
- * Print the SQL statement execution status
- *
- * @ Param judge SQL statement execution: Successful (YES)/failed (NO)
- * @ Param action SQL statement related operations: Create, insert, query, and delete
- */
- -(Void) judgeDBError :( BOOL) judge action :( NSString *) action
- {
- If (! Judge & [self. db hadError]) {
- // Print the error message if an error occurs.
- NSLog (@ "% @ failed: % @", action, [self. db lastError]);
- }
- Else
- {
- NSLog (@ "% @ success", action );
- }
- }
Execute Query
The SELECT command is the query, and the method for executing the query starts with-excuteQuery.
If the FMResultSet object is returned successfully when the query is executed, the nil is returned incorrectly. It is equivalent to executing the update. The NSError ** parameter is supported. You can also use-lastErrorCode and-lastErrorMessage to obtain the error message.
- FMResultSet *s = [db executeQuery:@"SELECT * FROM myTable"];
- while ([s next]) {
- //retrieve values for each record
- }
For example:
- -(NSMutableArray *)selectAllStudents
- {
- FMResultSet *set = [_db executeQuery:@"select * from student"];
- return [self selectAllStudentsHelper:set];
- }
For the versatility of data search, a general method is provided to search Student data.
- (NSMutableArray *) selectAllStudentsHelper :( FMResultSet *) FMSet;
-
- -(NSMutableArray *) selectAllStudentsHelper :( FMResultSet *) FMSet
- {
- NSMutableArray * selectResult = [NSMutableArray array];
- While ([FMSet next]) {
- Student * stu = [[Student alloc] init];
- Stu. number = [FMSet stringForColumn: @ "number"];
- Stu. name = [FMSet stringForColumn: @ "name"];
- Stu. age = [FMSet stringForColumn: @ "age"];
- /**
- * Different types of values have different methods.
- * Int a = [FMSet intForColumn: @ "a"],
- * NSString * B = [FMSet stringForColumn: @ "B"],
- * NSData * c = [FMSet dateForColumn: @ "c"],
- * Float d = [FMSet doubleForColumn: @ "d"],
- */
- [SelectResult addObject: stu];
- }
- Return selectResult;
- }
FMResultSet provides many methods to obtain the values in the desired format:
IntForColumn:
LongForColumn:
LongLongIntForColumn:
BoolForColumn:
DoubleForColumn:
StringForColumn:
DataForColumn:
DataNoCopyForColumn:
UTF8StringForColumnIndex:
ObjectForColumn:
Link: http://my.oschina.net/CgShare/blog/293635