Use of FMDB in iOS and iOSFMDB

Source: Internet
Author: User

Use of FMDB in iOS and iOSFMDB

1. In daily development, we need to use the offline cache to store data into the database and load data when there is no network. In IOS, we use the sqlite3 database, we can also use native SQL, but it is difficult to write, especially for programmers who transfer data from other languages. Next we will introduce a relatively good and simple third-party FMDB.

2 FMDB https://github.com/ccgus/fmdb

3. After the FMDB file is downloaded, you must import the following files in the project and use the libsqlite3.dylib dependency package.

4. Common FMDB classes

FMDatabase: A single SQLite database used to execute SQL statements.

FMResultSet: queries an FMDatabase result set.

FMDatabaseQueue: this class is used when multiple threads are used to execute queries and updates.

5. operate databases

1. Create and open a database

// 1 obtain the database object NSString * path = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0]; path = [path stringByAppendingPathComponent: @ "test. sqlite "]; dataBase = [FMDatabase databaseWithPath: path]; // 2 open the dataBase. if not, create and open BOOL open = [dataBase open]; if (open) {NSLog (@ "database opened successfully");} // 3 create a table NSString * create1 = @ "create table if not exists t_user (id integer autoincrement primary key, name Varchar) "; BOOL c1 = [dataBase executeUpdate: create1]; if (c1) {NSLog (@" table created successfully ");} // 4 insert data NSString * insertSql = @ "insert into t_user (id, name) values (?,?) "; // Insert Statement 1 bool inflag1 = [dataBase executeUpdate: insertSql, @ (2), @" admin "]; // insert Statement 2 bool inflag2 = [dataBase executeUpdate: insertSql withArgumentsInArray: @ [@ "admin", @ (5)]; // insert Statement 3 bool inflag3 = [dataBase executeUpdateWithFormat: @ "insert into t_user (id, name) values (% @, % d) ", @" admin ", 6]; // delete statement NSString * delete = @" delete from t_user "; BOOL dflag = [dataBase executeUpdate: delete]; if (dflag) {NSLog (@ "delete ");} // Modify the NSString * update = @" update t_user set name =? "; BOOL flag = [dataBase executeUpdate: update, @" zhangsan "]; if (flag) {NSLog (@" modified successfully ");} // 5 FMResultSet for Data Query FMDB provides multiple methods to obtain different types of data
NSString * sql=@" select * from t_user ";FMResultSet *result=[dataBase executeQuery:sql];while(result.next){int ids=[result intForColumn:@"id"];NSString * name=[result stringForColumn:@"name"];int ids=[result intForColumnIndex:0];NSString * name=[result stringForColumnIndex:1];NSLog(@"%@,%d",name,ids);         }

If the application uses multiple threads to operate the database, you need to use FMDatabaseQueue to ensure thread security. An FMDatabase object cannot be used in multiple threads in an application to operate on the database. This will cause database data confusion. To ensure the security of multi-threaded database operations, FMDB uses FMDatabaseQueue. It is very simple to use FMDatabaseQueue. First, a database file address is used to initialize FMDatabaseQueue, and then a closure (block) can be set) passed in the inDatabase method. Operate the database in the closure, instead of directly managing the FMDatabase.

// 2 multi-threaded NSString * path = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0]; path = [path stringByAppendingPathComponent: @ "test. sqlite "]; FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath: path]; [queue inDatabase: ^ (FMDatabase * db) {NSString * create = @ "create table if not exists t_book (id integer, name varchar)"; BOOL c1 = [db executeUpdate: create]; if (c1) {N SLog (@ "succeeded") ;}}]; [queue inDatabase: ^ (FMDatabase * db) {NSString * insertSql = @ "insert into t_book (id, name) values (?,?) "; // Insert Statement 1 bool inflag = [db executeUpdate: insertSql, @ (2), @" admin "]; if (inflag) {NSLog (@ "inserted successfully") ;}}]; [queue inDatabase: ^ (FMDatabase * db) {FMResultSet * data = [db executeQuery: @ "select * from t_book"]; while (data. next) {int ids = [data intForColumn: @ "id"]; NSString * name = [data stringForColumn: @ "name"]; NSLog (@ "% @", name); NSLog (@ "% I", ids) ;}}];

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.