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, 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;

TheThe 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, 51cto, which I think is so loving and hateful. The articles referenced by it have never been mentioned in the source. All are region names. This is quite speechless. I accidentally discovered that what I wrote was labeled as a nickname?

1. First, instantiate 1.Fmdatabase objects.Sqlitepersistentobjects
Operations on a subclass are different. 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 in IOS. <br/> nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes ); <br/> nsstring * documentdirectory = [paths objectatindex: 0]; <br/> // dbpath: Database path, in document. <Br/> nsstring * dbpath = [documentdirectory stringbyappendingpathcomponent: @ "test. DB "]; <br/> // create a database instance dB here: if the path does not exist" test. database file, SQLite will automatically create "test. DB "<br/> fmdatabase * DB = [fmdatabase databasewithpath: dbpath]; <br/> If (! [DB open]) {<br/> nslog (@ "cocould not open dB."); <br/> return; <br/>} 

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. <br/> [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 text type in OC corresponds to nsstring integer corresponding to nsnumber integer <br/> [dB executeupdate: @ "insert into user (name, age) values (?,?) ", @" Wife ", [nsnumber numberwithint: 20]

Next, update the data.

 

// Update the data and change "wife" to "baby" <br/> [dB executeupdate: @ "update user set name =? Where name =? ", @" Wife ", @" baby "];

Next, we will delete the data.

// Delete data <br/> [dB executeupdate: @ "delete from user where name =? ", @" Wife "];

 

The basic update operations are as follows: queries!

// Return the first result in the database that meets the conditions <br/> 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.

Fmresultset * rs = [dB executequery: @ "select * from user"]; <br/> rs = [dB executequery: @ "select * from user where age =? ", @" 20 "]; <br/> while ([RS next]) {<br/> nslog (@" % @ ", [RS stringforcolumn: @ "name"], [RS stringforcolumn: @ "Age"]); <br/>}

 

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.