Use of FMDB, use of FMDB

Source: Internet
Author: User

Use of FMDB, use of FMDB

Use of FMDB

I. Three Important FMDB classes

1.1 FMDataBase: Provides SQLite database classes for executing SQL statements.

1.2 FMResultSet: class used to execute query results in FMDatabase

1.3 FMDatabaseQueue: class used to query and update databases under multiple threads

 

Ii. Use FMDB

2.1 create a database

FMDatabase * db = [FMDatabase databaseWithPath: @ "/tmp. db"];

2.2 Open a database

2.3 database update (except for the select statement, it can be called an update operation)

2.4 Database Query

Execute the select statement by calling-executeQuery.

A FMResultSet object is returned if the result is successful.

Nil is returned if the request fails.

(You can use the-lastErrorMessage and-lastErrorCode methods to confirm why the query fails)

To traverse the query results, you need to loop through while () and view records one by one. In FMDB, the following simple method can be used:

FMResultSet * s = [db executeQuery: @ "SELECT * FROM myTable"];

While ([s next]) {

.....

}

 

Even if you only need to obtain one data, you must call-[FMResultSet next] before accessing the query results.

FMResultSet * s = [db executeQuery: @ "select count (*) FROM myTable"];

If ([s next]) {

Int totalCount = [s intForColumnIndex: 0];

}

 

FMResultSet provides many convenient methods to query data:

  • IntForColumn:
  • LongForColumn:
  • LongLongIntForColumn:
  • BoolForColumn:
  • DoubleForColumn:
  • StringForColumn:
  • DateForColumn:
  • DataForColumn:
  • DataNoCopyForColumn:
  • UTF8StringForColumn:
  • ObjectForColumn:

Generally, there is no need to manually close an FMResultSet because the result set is released or the source database is closed automatically.

2.5 shut down the database

[Db close];

 

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.