Use a third-party framework FMDB for Sqlite3 in iPhone Data Storage

Source: Internet
Author: User

IPhone dataAboutSqlite3. Use a third-party frameworkFMDBIs the content to be introduced in this article.IPhoneUseSqliteProceedDataStorage is a habit. It is generally used to other platforms.SqliteFor example, android.

Some encapsulated third-party frameworks are available on the iphone, which saves a lot of time. For example, Sqlitepersistentobjects,FMDB. I searched for these two frameworks today.FMDBThe style is more in line with my use, in fact, both have their own advantages, just look at your personal preferences. The following are some basic functions of FMDB. The FMDB framework is actually a thin encapsulation layer, and there are two main classes: FMDatabase and FMResultSet;

The FMResultSet object reminds me of the sqlite cursor set in android.

The github address of FMDB is https://github.com/ccgus/fmdb.

1. First, an FMDatabase object must be instantiated. This is different from Sqlitepersistentobjects that derives a subclass for operations. Next, open a database and create a database if it does not exist)

 
 
  1. // Paths: Specifies the Document path in ios. The Document is a folder that can be read and written in ios.
  2. NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
  3. NSString * documentDirectory = [paths objectAtIndex: 0];
  4. // DbPath: Specifies the database path in Document.
  5. NSString * dbPath = [documentDirectory stringByAppendingPathComponent: @ "Test. db"];
  6. // Create a database instance db. If the "Test. db" file does not exist in the path, sqlite automatically creates "Test. db"
  7. FMDatabase * db = [FMDatabase databaseWithPath: dbPath];
  8. If (! [Db open]) {
  9. NSLog (@ "cocould not open db .");
  10. Return;
  11. }
  12. // Paths: Specifies the Document path in ios. The Document is a folder that can be read and written in ios.
  13. NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
  14. NSString * documentDirectory = [paths objectAtIndex: 0];
  15. // DbPath: Specifies the database path in Document.
  16. NSString * dbPath = [documentDirectory stringByAppendingPathComponent: @ "Test. db"];
  17. // Create a database instance db. If the "Test. db" file does not exist in the path, sqlite automatically creates "Test. db"
  18. FMDatabase * db = [FMDatabase databaseWithPath: dbPath];
  19. If (! [Db open]) {
  20. NSLog (@ "cocould not open db .");
  21. Return;
  22. }

Next, we can operate on this database object. The operations are update and queries.

First, create a table.

 
 
  1. // Create a table named User with two fields: string-type Name and integer-type Age.
  2. [Db executeUpdate: @ "create table User (Name text, Age integer)"];
  3. // Create a table named User with two fields: string-type Name and integer-type Age.
  4. [Db executeUpdate: @ "create table User (Name text, Age integer)"];

In this way, we have a table. Next we will operate the table. Insert data! Note that the inserted data uses wildcards, which is the same as the Bind Variable directly on the iphone using sqlite. The data following the wildcard matches.

 
 
  1. // Insert data using the type text in OC corresponding to NSString integer corresponding to NSNumber integer
  2. [Db executeUpdate: @ "insert into User (Name, Age) VALUES (?,?) ", @" Wife ", [NSNumber numberWithInt: 20]
  3. // Insert data using the type text in OC corresponding to NSString integer corresponding to NSNumber integer
  4. [Db executeUpdate: @ "insert into User (Name, Age) VALUES (?,?) ", @" Wife ", [NSNumber numberWithInt: 20]

Next, update the data.

 
 
  1. // Update the data and change "wife" to "baby"
  2. [Db executeUpdate: @ "UPDATE User SET Name =? WHERE Name =? ", @" Wife ", @" baby "];
  3. // Update the data and change "wife" to "baby"
  4. [Db executeUpdate: @ "UPDATE User SET Name =? WHERE Name =? ", @" Wife ", @" baby "];

Next, we will delete the data.

 
 
  1. // Delete data
  2. [Db executeUpdate: @ "delete from User WHERE Name =? ", @" Wife "];
  3. // Delete data
  4. [Db executeUpdate: @ "delete from User WHERE Name =? ", @" Wife "];

The basic update operations are as follows: queries!

 
 
  1. // Return the first matching result in the database
  2. NSString * aa = [db stringForQuery: @ "SELECT Name FROM User WHERE Age =? ", @" 20 "];
  3. // Return the first matching result in the database
  4. NSString * aa = [db stringForQuery: @ "SELECT Name FROM User WHERE Age =? ", @" 20 "];

In this way, a piece of data is returned for the query. What should we do if we want to query multiple pieces of data? Don't worry, I mentioned another major class in FMDB, FMResultSet. This is a result set! When multiple data entries are returned, FMDB places the data in this result set. Then, we are querying this result set! Very simple.

 
 
  1. FMResultSet *rs=[db executeQuery:@"SELECT * FROM User"];    
  2. rs=[db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"20"];    
  3. while ([rs next]){    
  4. NSLog(@"%@ %@",[rs stringForColumn:@"Name"],[rs stringForColumn:@"Age"]);    
  5. }    
  6. FMResultSet *rs=[db executeQuery:@"SELECT * FROM User"];  
  7. rs=[db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"20"];  
  8. while ([rs next]){  
  9. NSLog(@"%@ %@",[rs stringForColumn:@"Name"],[rs stringForColumn:@"Age"]);  
  10. }  

More FMResultSet methods include:

 
 
  1. intForColumn:   
  2. longForColumn:   
  3. longLongIntForColumn:   
  4. boolForColumn:   
  5. doubleForColumn:   
  6. stringForColumn:   
  7. dateForColumn:   
  8. dataForColumn:   
  9. dataNoCopyForColumn:   
  10. UTF8StringForColumnIndex:   
  11. objectForColumn:  

Just check the class! Okay,FMDBIs this simple? In fact, this EncapsulationSqliteAs long as you have mastered SQL!

Summary:IPhone dataAboutSqlite3. Use a third-party frameworkFMDBI hope this article will help you!

Related Article

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.