iOS Development database Chapter-fmdb Database queue

Source: Internet
Author: User

iOS Development database Chapter-fmdb Database queue

One, code example

1. You need to import the Fmdb framework and header files first, since the framework relies on the Libsqlite library, you should also import the library.

2. The code is as follows:


1//
2//YYVIEWCONTROLLER.M
3//05-fmdb database queue
4//
5//Created by Apple on 14-7-28.
6//Copyright (c) 2014 wendingding. All rights reserved.
7//
8
9 #import "YYViewController.h"
Ten #import "FMDB.h"
11
@interface Yyviewcontroller ()
@property (Nonatomic,strong) Fmdatabasequeue *queue;
14  @end15 @implementation YYViewController17-(void) VIEWDIDLOAD19 {[Super viewdidload];21 22//1. Get the path to the database file Dia. nsstring *doc=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject];24 NSString *filename=[doc stringbyappendingpathcomponent:@ "Person.sqlite"];25 26//2. Get Database Queue Fmdatabasequeue *queue= [Fmdatabasequeue databasequeuewithpath:filename];28//fmdatabase *db=[fmdatabase databasewithpath:filename];29 30// 3. Open Database [Queue indatabase:^ (Fmdatabase *db) {BOOL result=[db executeupdate:@ "CREATE TABLE IF not EXISTS T_person (ID Integer PRIMARY KEY autoincrement, name text NOT NULL, age integer NOT NULL); "]; if (result) {NSLog (@ "creation success"),}else36 {PNS NSLog (@ "Failure of the Table"),}39}];40 self.queue=queue;41 (}43)-(void) Touchesb Egan: (Nsset *) touches withevent: (uievent *) event45 {46//Insert data//[Self.queue indatabase:^ (Fmdatabase *db) {//[db EXE] cuteupdate:@ "INSERT into T_person (name, age) VALUES (?,?);", @ "wendingding", @22];49}];50 51//query data [self.queue indatabase:^ (Fmdatabase *db) {53//1. Execute query statement, fmresultset *resultset = [db executeQuery: @ "SELECT * from T_person"];55 56//2. Traverse result ([ResultSet next]) {$ int ID = [resultSet intforcolumn:@ ' id '];59 nsstr ing *name = [resultSet stringforcolumn:@ ' name '];60 int age = [ResultSet intforcolumn:@ ' age '];61 NSLog (@ "%d%@%d", ID, Nam E, age);}63}];64}66 @end

Insert the data first, then query the result, print as follows:

3. Code description

With a queue object, it automatically has a database object inside it, and the operation of the database is thread-safe.second, the businessTransaction, there is a problem if there is no transaction. Example: bank example Zhang San and John Doe account have 1000 yuan, if Zhang three to transfer to John Doe, need to execute two SQL statements, considering security, both of these fish have to either execute successfully, or all failed to execute. Transactions: Put multiple statements in the same transaction, either all succeed or all fail (and automatically rollback if there is a problem in the middle).  The execution of a transaction is atomic. Transaction code Processing: Add multiple statements to a transaction to execute:
1    //Insert Data 2     [Self.queue indatabase:^ (Fmdatabase *db) {3         [db begintransaction];4         [db executeupdate:@] Insert into T_person (name, age) VALUES (?,?), "@" wendingding ", @22];5         [db executeupdate:@" INSERT into T_person (name , age) VALUES (?,?), "@ wendingding", @23];6         [db executeupdate:@ "INSERT into T_person (name, age) VALUES (?,?);", @ "W Endingding ", @24];7         [db executeupdate:@" INSERT into T_person (name, age) VALUES (?,?); ", @" wendingding ", @25];8         [DB Commit];9     }];

If a problem occurs halfway, it is automatically rolled back, or you can choose to roll back manually.

1     //Insert Data 2     [Self.queue indatabase:^ (Fmdatabase *db) {3         [db BeginTransaction]; 4         [DB executeupdate:@] INSERT into T_person (name, age) VALUES (?,?); ", @" wendingding ", @22]; 5         [db executeupdate:@ "INSERT into T_person (name, age) VALUES (?,?);", @ "wendingding", @23]; 6         [db executeupdate: @ "INSERT into T_person (name, age) VALUES (?,?);", @ "wendingding", @24]; 7         [db rollback]; 8         [db executeupdate:@ "INSERT into T_person (name, age) VALUES (?,?);", @ "wendingding", @25]; 9
   [db commit];10     }];

The code above. The first three INSERT statements are obsolete.

Another way to transact:

1    [Self.queue intransaction:^ (Fmdatabase *db, BOOL *rollback) {2         [db executeupdate:@ "INSERT into T_person (name , age) VALUES (?,?), "@ wendingding", @22];3         [db executeupdate:@ "INSERT into T_person (name, age) VALUES (?,?);", @ "W Endingding ", @23];4         [db executeupdate:@" INSERT into T_person (name, age) VALUES (?,?); ", @" wendingding ", @24];5     }];

Description: Open a transaction, start the transaction, then execute the code snippet in the block, and then commit the transaction.

iOS Development database Chapter-fmdb Database queue

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.