IOS-FMDB database additions and deletions to check the use of

FMDB is a lightweight database used to store network resources locally. FMDB SQLite API had a very friendly package, very easy to use, for those who use pure Sqlite API for database operation app, it can be considered to migrate to FMDB based on the database for future development, function maintenance, can improve the efficiency of many.

What is the FMDB
FMDB is the iOS platform SQLite database framework
FMDB in a OC way to encapsulate the SQLite language API C

The advantage of
FMDB to use more object oriented, eliminating the Core Data framework contrast apple C language code
a lot of trouble, the built-in redundancy, more lightweight and flexible
provides a database operation method of multi thread safe, effectively prevent data confusion

FMDB has three major core classes:

FMDatabase
, a FMDatabase object represents a single SQLite
database is used to execute SQL statements FMResultSet
FMDatabase after executing the query result set FMDatabaseQueue
to execute a plurality of queries or updates in multiple threads, it is thread safe

Path file path in three cases:

If there is no specific file path
will automatically create an empty string @ “
in the temporary directory to create an empty database
FMDatabase when the connection is closed, the database file has been deleted nil
will create a number of temporary memory in a database, when FMDatabase connection is closed, the database will be destroyed

Introduce the FMDB third party library to the project

Will FMDB down down and then copy to your project.
note: the need for libsqlite3.dylib dependent (specific process Project-> TARGETS-> Build Phases-> Link Binary With Libraries, and then click + number to refer to the dynamic library)

IOS-FMDB database additions and deletions to check the use of

Two. Database operation: additions and deletions

Usually the operation of the database, we generally referred to as CURD, namely the table to create (Create), update (Update), read (Read) and delete (Delete) operations.
FMDB Tim excision check operation based on the add delete change operation is very simple, do not need to do too much, only need to use the executeUpdate method of FMDB packaged on the line.

The stored in the database for each value has a type 1 NULL the value of 2 INTEGER value is identified as integer is empty, according to the value of 1 to 8 can be stored in a 3 byte REAL all values are floating value 4 TEXT value 5 BLOB text string value for the BLOB data

Here to write a Demo, test it!

IOS-FMDB database additions and deletions to check the use of
#import "FMDatabase.h" #import "FMDatabaseQueue.h"

Gets the path to the database file:

NSString *doc = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *path = [doc stringByAppendingPathComponent:@ = "user.sqlite"]; self.dbPath path; NSLog (@ "dbPath---%@", path);

Build table:

/ / built table (void) - createTable {NSLog (@ "%s", __func__); NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:self.dbPath] = = NO) {/ / create it FMDatabase *db = [FMDatabase databaseWithPath: self.dbPath]; if ([db open]) {NSString *sql = CREATE TABLE'User'@ "('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,'name'VARCHAR (30),'password' VARCHAR (30)); BOOL res = [db executeUpdate:sql]; if (! RES) {NSLog (" error when creating DB @ table ");} else {NSLog (" success to creating DB @ table ");} [db {NSLog} else (close];" error when open @ DB); }}

Insert data:

/ / insert data - (void) insertData {NSLog (@ "%s", __func__); static int IDX = 1; FMDatabase = *db [FMDatabase databaseWithPath:self.dbPath]; if ([db open]) {NSString *sql = @ "insert into user (name, password) values (?,?)"; NSString *name = [NSString stringWithFormat:@ ZL%d, idx++]; BOOL res = [db executeUpdate:sql, name, @ "girl"]; if (! RES) {NSLog (@ error to insert data ");} else {NSLog (@ success to insert data"); ZLTestModel *model = [ZLTestModel modelWith:name id:idx]; [self.userArr addObject:model]; [self.tableView reloadData];} [db close];}}

Update data:

/ / update data - (void) updateData {NSLog (@ "%s", __func__); FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; if ([db open]) {NSString *sql = @ "UPDATE USER SET id = WHERE? Name = BOOL; res = [db?" executeUpdate: SQL, "1", "@ @ ZL"]; if (! RES) {NSLog (@ error to UPDATE data ");} else {NSLog (@ success to UPDATE data"); [self queryData];} [db close];}}

Delete data:

/ / delete data - (void) deleteData {NSLog (@ "%s", __func__); FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; if ([db open]) {NSString *sql = @ "delete from user"; BOOL res = [db executeUpdate:sql]; if (! RES) {NSLog ("error to delete DB @ data) else;} {NSLog (" success to delete DB @ data "); [self.userArr removeAllObjects] [self.tableView; reloadData] [db close];};}}

Query data:

/ / data query (void) - queryData {NSLog (@ "%s", __func__); FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; if ([db open]) {NSString *sql = @ "select *from user"; FMResultSet *rs = [db executeQuery:sql]; while (RS [next]) {int userId = [rs intForColumn:@ "Id"]; NSString *name [rs stringForColumn:@ = "name"]; NSString *pass = [rs stringForColumn:@ "password"]; NSLog ("user @ id =%d, name = pass =% @% @, userId, name, pass, ZLTestModel); *model = [ZLTestModel modelWith:name id:userId]; [self.userArr addObject:model]; [self.tableView reloadData];} [db close];}}

Multithread:

- (void) multithread {NSLog (@ "%s", __func__); FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:self.dbPath]; dispatch_queue_t Q1 = dispatch_queue_create (queue1, NULL); dispatch_queue_t Q2 = dispatch_queue_create (Queue2, NULL); dispatch_async (Q1, for ^{(int i = 0; I 100; < ++i [queue inDatabase:^ (FMDatabase) {*db {*sql = "@ NSString) insert into user (name, password) values (?,?)"; NSString *name = [NSString stringWithFormat:@ "queue111%d", i] BOOL [db; res = executeUpdate:sql, name, @ "boy"]; if ({NSLog (RES!) "error to add DB data: @% @", name);} else {NS Log ("success to add DB data: @% @", name);}}});}]; dispatch_async (Q2, for ^{(int i = 0; I 100; < ++i) {[queue inDatabase:^ (FMDatabase *db) {NSString *sql = insert into user (@ name, password (values)?,?) "; NSString *name = [NSString stringWithFormat:@" queue222%d ", i] BOOL [db; res = executeUpdate:sql, name, @" boy "]; if (! RES) {NSLog (" error to add DB data: @% @ ", name);} else {NSLog (" to add DB @ success data:, name);% @}]}};}});