FMDB for IOS data storage, iosfmdb

Source: Internet
Author: User

FMDB for IOS data storage, iosfmdb

FMDB is a third-party framework for Data storage. Compared with SQLite and Core Data, FMDB has many advantages.

FMDB Is Object-Oriented. It encapsulates the C language API of SQLite in OC mode, which is more convenient to use and does not require much knowledge about database operations. However, it also has some problems, such as cross-platform. Because it is encapsulated in the oc language, it can only be used during ios development. If you want to implement cross-platform operations, to reduce development costs and maintenance costs, you need to use the original SQLite.

Core Data is an embodiment of ORM. Using Core Data requires the conversion of model Data. Although the operation is simple, you do not need to directly operate the database, but the performance is not directly high with SQLite. However, when using SQLite, you need to use functions in the C language, which is difficult to operate. Therefore, you need to encapsulate it. However, if it is simply encapsulated, it is likely to ignore many important details, such as how to handle concurrency and security issues.

Therefore, we recommend that you use a third-party framework, FMDB, which is an encapsulation of the libsqlite3 framework. The procedure is similar to that used by SQLite, in addition, it processes a table while operating on multiple threads, which means it is thread-safe. FMDB is a lightweight framework and flexible to use. It is the first choice for development by many enterprises.

Important classes in FMDB

FMDatabase: An FMDatabase object represents a separate SQLite database used to execute SQL statements.

FMResultSet: The result set after the query is executed using FMDatabase

FMDatabaseQueue: used to execute multiple queries or updates in multiple threads. It is thread-safe.

Procedure

1. Download The FMDB file fmdb and add the FMDB folder to the project.

2. Import the sqlite framework and FMDatabase. h file

 

3. Similar to using SQLite, You need to obtain the path of the database file, obtain the database, open the database, perform operations on the database, and close the database. The Code is as follows:

// ViewController. m // JRFMDB // Created by jerehedu on 15/6/18. // Copyright (c) 2015 jerehedu. all rights reserved. // # import "ViewController. h "# import" FMDatabase. h "@ interface ViewController () @ property (nonatomic, strong) FMDatabase * db; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // import sqlite framework, import the FMDB folder // 1. obtain the path of the database file NSString * doc = [NSSearchPathForDirectoriesInDoma Ins (NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString * fileName = [doc stringByAppendingPathComponent: @ "student. sqlite "]; NSLog (@" fileName = % @ ", fileName); // 2. obtain the database FMDatabase * db = [FMDatabase databaseWithPath: fileName]; // 3. open Database if ([db open]) {NSLog (@ "OK"); // 4. create table bool result = [db executeUpdate: @ "create table if not exists t_student (id integer primary key autoincrement, name te Xt not null, age integer not null); "]; if (result) {NSLog (@" table created successfully ");} else {NSLog (@ "failed to create table") ;}} self. db = db; // insert data [self insertStu]; [self deleteStu: 6]; [self updateStu: @ "apple7_name": @ "7777"]; [self queryStu]; [self dropStu]; [self insertStu]; [self queryStu]; // 6. close database [self. db close] ;}# pragma mark insert data-(void) insertStu {for (int I = 0; I <10; I ++) {NSString * name = [NSString stringWithFormat: @ "1a Pple % I _name ", I]; int age = arc4random () % 3 + 20; // 1. executeUpdate: Unknown Parameter used? [Self. db executeUpdate: @ "insert into t_student (name, age) VALUES (?,?); ", Name, @ (age)]; // 2. executeUpdateWithFormat: uncertain parameters include % @, % d, and so on (the parameter is of the original data type) // [self. db executeUpdateWithFormat: @ "insert into t_student (name, age) values (% @, % I);", name, age]; // 3. array // [self. db executeUpdate: @ "insert into t_student (name, age) VALUES (?,?); "WithArgumentsInArray: @ [name, @ (age)] ;}# pragma mark deletes data-(void) deleteStu :( int) idNum {//. executeUpdate: used for uncertain parameters? To placeholder (all subsequent parameters must be oc objects) // [self. db executeUpdate: @ "delete from t_student where id = ?; ", @ (IdNum)]; // B. executeUpdateWithFormat: uncertain parameters include % @ and % d. // [self. db executeUpdateWithFormat: @ "delete from t_student where name = % @;", @ "apple9_name"] ;}# pragma mark destroy table-(void) dropStu {[self. db executeUpdate: @ "drop table if exists t_student;"]; // 4. create Table BOOL result = [self. db executeUpdate: @ "create table if not exists t_student (id integer primary key autoincrement, name text not null, age integer NO T null); "]; if (result) {NSLog (@" ");} else {NSLog (@" ");}} # pragma mark modify data-(void) updateStu :( NSString *) oldName :( NSString *) newName {// [self. db executeUpdateWithFormat: @ "update t_student set name = % @ where name = % @;", newName, oldName]; [self. db executeUpdate: @ "update t_student set name =? Where name =? ", NewName, oldName] ;}# pragma mark query data-(void) queryStu {// 1. execute the query statement // FMResultSet * resultSet = [self. db executeQuery: @ "select * from t_student;"]; FMResultSet * resultSet = [self. db executeQuery: @ "select * from t_student where id <?; ", @ (14)]; // 2. traversal result set while ([resultSet next]) {int idNum = [resultSet intForColumn: @ "id"]; NSString * name = [resultSet objectForColumnName: @ "name"]; int age = [resultSet intForColumn: @ "age"]; NSLog (@ "id = % I, name = % @, age = % I", idNum, name, age) ;}}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end

 

For questions or technical exchanges, please join the official QQ group: (452379712)

 

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

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.