Data storage on iPhone sqlite3-third-party framework FMDB

Source: Internet
Author: User

Using sqlite for data storage on the iPhone is a habit. Sqlite, such as android, is also used to other platforms.

Some encapsulated third-party frameworks are available on the iphone, which saves a lot of time. For example, Sqlitepersistentobjects and FMDB. Today, I searched for these two frameworks. I feel that the FMDB style is more in line with my use. In fact, they both have their own advantages, but they only look at their 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.

Supplement: After importing FMDB, remember to import the sqlite3Framework, libsqlite3.0.dylib, and PS of iOS: I found that some of my blog posts are reposted on the Internet, after all, some of my blog posts summarize others' experiences. However, there is a website called BKJIA, which I think is so loving and hateful. The articles it cited have never been mentioned. All are region names. This is quite speechless. I accidentally discovered that what I wrote was labeled as a nickname?

1. First, an FMDatabase object must be instantiated. This is different from Sqlitepersistentobjects that derives a subclass for operations. Next, open a database (if not, a database will be created)

// Paths: The Document path in ios. The Document is a folder that can be read and written by ios.

// Create a database instance db. Note: if the "Test. db" file does not exist in the path, sqlite will automatically create "Test. db"

 
 
  1. NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
  2. NSString * documentDirectory = [paths objectAtIndex: 0];
  3. // DbPath: Specifies the database path in Document.
  4. NSString * dbPath = [documentDirectory stringByAppendingPathComponent: @ "Test. db"];
  5. FMDatabase * db = [FMDatabase databaseWithPath: dbPath];
  6. If (! [Db open]) {
  7. NSLog (@ "cocould not open db .");
  8. Return;
  9. }

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

First, create a table.

// Create a table named User with two fields: string-type Name and integer-type Age.

 
 
  1. [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.

// Insert data using the type text in OC corresponding to NSString integer corresponding to NSNumber integer

 
 
  1. [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 "];

Next, we will delete the data.

 
 
  1. // Delete data
  2. [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 "];

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"]);  

More FMResultSet methods include:

IntForColumn:

LongForColumn:

LongLongIntForColumn:

BoolForColumn:

DoubleForColumn:

StringForColumn:

DateForColumn:

DataForColumn:

DataNoCopyForColumn:

UTF8StringForColumnIndex:

ObjectForColumn:

Just check the class! Well, is it very easy to use FMDB? In fact, the sqlite encapsulated frameworks have never changed, as long as you have mastered the SQL statements!

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.