FMDB for iOS third parties

Source: Internet
Author: User

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

 
 
  1. <Pre name = "code" class = "objc"> if (! [_ Db open]) {
  2. NSLog (@ "database opening failed ");
  3. Return NO;
  4. }
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

 
 
  1. // Two SQL statement writing methods
  2.  
  3. NSString * sqlStr = [NSString stringWithFormat: @ "insert into student values ('% @', '% @', '% @')", student. number, student. name, student. age];
  4. NSLog (@ "% @", sqlStr );
  5. 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];
  6.  
  7. [Self judgeDBError: result action: DATABASE_INSERT]; return result;
  8.  
  9.  
  10. /**
  11. * Print the SQL statement execution status
  12. *
  13. * @ Param judge SQL statement execution: Successful (YES)/failed (NO)
  14. * @ Param action SQL statement related operations: Create, insert, query, and delete
  15. */
  16. -(Void) judgeDBError :( BOOL) judge action :( NSString *) action
  17. {
  18. If (! Judge & [self. db hadError]) {
  19. // Print the error message if an error occurs.
  20. NSLog (@ "% @ failed: % @", action, [self. db lastError]);
  21. }
  22. Else
  23. {
  24. NSLog (@ "% @ success", action );
  25. }
  26. }
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.

 
 
  1. FMResultSet *s = [db executeQuery:@"SELECT * FROM myTable"];    
  2. while ([s next]) {    
  3.     //retrieve values for each record    

For example:

 
 
  1. -(NSMutableArray *)selectAllStudents 
  2.     FMResultSet *set = [_db executeQuery:@"select * from student"]; 
  3.     return [self selectAllStudentsHelper:set]; 

For the versatility of data search, a general method is provided to search Student data.

 
 
  1. (NSMutableArray *) selectAllStudentsHelper :( FMResultSet *) FMSet;
  2.  
  3. -(NSMutableArray *) selectAllStudentsHelper :( FMResultSet *) FMSet
  4. {
  5. NSMutableArray * selectResult = [NSMutableArray array];
  6. While ([FMSet next]) {
  7. Student * stu = [[Student alloc] init];
  8. Stu. number = [FMSet stringForColumn: @ "number"];
  9. Stu. name = [FMSet stringForColumn: @ "name"];
  10. Stu. age = [FMSet stringForColumn: @ "age"];
  11. /**
  12. * Different types of values have different methods.
  13. * Int a = [FMSet intForColumn: @ "a"],
  14. * NSString * B = [FMSet stringForColumn: @ "B"],
  15. * NSData * c = [FMSet dateForColumn: @ "c"],
  16. * Float d = [FMSet doubleForColumn: @ "d"],
  17. */
  18. [SelectResult addObject: stu];
  19. }
  20. Return selectResult;
  21. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.