IOS development: FMDB data storage Analysis
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.
FMDB is a third-party framework for Data storage. Compared with SQLite and Core Data, FMDB has many advantages.
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 the sqlite framework and the FMDB folder
// 1. Obtain the path of the database file
NSString * doc = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString * fileName = [doc stringByAppendingPathComponent: @ "student. sqlite"];
NSLog (@ "fileName = % @", fileName );
// 2. Obtain the database
FMDatabase * db = [FMDatabase databaseWithPath: fileName];
// 3. Open the database
If ([db open]) {
NSLog (@ "OK ");
// 4. Create a table
BOOL result = [db executeUpdate: @ "create table if not exists t_student (id integer primary key autoincrement, name text not null, age integer not null);"];
If (result ){
NSLog (@ "table created successfully ");
} Else {
NSLog (@ "Table creation failed ");
}
}
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 the database
[Self. db close];
}
# Pragma mark insert data
-(Void) insertStu
{
For (int I = 0; I <10; I ++)
{
NSString * name = [NSString stringWithFormat: @ "1 apple % I _name", I];
Int age = arc4random () % 3 + 20;
// 1. executeUpdate: used for uncertain parameters? (All parameters must be oc objects)
[Self. db executeUpdate: @ "insert into t_student (name, age) VALUES (?,?); ", Name, @ (age)];
// 2. executeUpdateWithFormat: uncertain parameters use % @, % 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 delete data
-(Void) deleteStu :( int) idNum
{
// A. executeUpdate: used for uncertain parameters? (All parameters must be oc objects)
// [Self. db executeUpdate: @ "delete from t_student where id = ?; ", @ (IdNum)];
// B. executeUpdateWithFormat: uncertain parameters are placeholder with % @, % d, etc.
// [Self. db executeUpdateWithFormat: @ "delete from t_student where name = % @;", @ "apple9_name"];
}
# Pragma mark destroy the table
-(Void) dropStu
{
[Self. db executeUpdate: @ "drop table if exists t_student;"];
// 4. Create a table
BOOL result = [self. db executeUpdate: @ "create table if not exists t_student (id integer primary key autoincrement, name text not null, age integer not null);"];
If (result ){
NSLog (@ "table created again ");
} Else {
NSLog (@ "failed to create a table again ");
}
}
# 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
// 2. traverse the 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) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@ End