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.M3 //05-fmdb Database Queue4 //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" One  A @interfaceYyviewcontroller () -@property (nonatomic,strong) Fmdatabasequeue *queue; - @end the  - @implementationYyviewcontroller -  -- (void) Viewdidload + { - [Super Viewdidload]; +      A     //1. Get the path to the database file atNSString *doc=[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; -NSString *filename=[doc stringbyappendingpathcomponent:@"Person.sqlite"]; -      -     //2. Get the database queue -Fmdatabasequeue *queue=[Fmdatabasequeue databasequeuewithpath:filename]; - //fmdatabase *db=[fmdatabase Databasewithpath:filename]; in      -     //3. Open the Database to[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); /c5>"]; -         if(Result) { theNSLog (@"success in the creation of a table"); *}Else $         {Panax NotoginsengNSLog (@"failed to create a table"); -         } the     }]; +Self.queue=queue; A      the } +  --(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event $ { $     //Inserting Data - //[Self.queue indatabase:^ (Fmdatabase *db) { - //[db executeupdate:@ INSERT into T_person (name, age) VALUES (?,?); ", @" wendingding ", @22]; the //    }]; -     Wuyi     //Querying Data the[Self.queue indatabase:^ (Fmdatabase *db) { -         //1. Execute the query statement WuFmresultset *resultset = [db executeQuery:@"SELECT * from T_person"]; -  About         //2. Traverse the results $          while([ResultSet next]) { -             intID = [ResultSet intforcolumn:@"ID"]; -NSString *name = [ResultSet stringforcolumn:@"name"]; -             intAge = [ResultSet intforcolumn:@" Age"]; ANSLog (@"%d%@%d", ID, name, age); +         } the     }]; -  $ } the  the @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 (if there is a problem in the middle, it will be rolled back automatically).  The execution of a transaction is atomic. Transaction code Processing: Add multiple statements to a transaction to execute:
1    //Inserting Data2[Self.queue indatabase:^ (Fmdatabase *db) {3 [DB BeginTransaction];4[DB executeupdate:@"INSERT into T_person (name, age) VALUES (?,?);",@"wendingding", @ A];5[DB executeupdate:@"INSERT into T_person (name, age) VALUES (?,?);",@"wendingding", @ at];6[DB executeupdate:@"INSERT into T_person (name, age) VALUES (?,?);",@"wendingding", @ -];7[DB executeupdate:@"INSERT into T_person (name, age) VALUES (?,?);",@"wendingding", @ -];8 [DB commit];9}];

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

1     //Inserting Data2[Self.queue indatabase:^ (Fmdatabase *db) {3 [DB BeginTransaction];4[DB executeupdate:@"INSERT into T_person (name, age) VALUES (?,?);",@"wendingding", @ A];5[DB executeupdate:@"INSERT into T_person (name, age) VALUES (?,?);",@"wendingding", @ at];6[DB executeupdate:@"INSERT into T_person (name, age) VALUES (?,?);",@"wendingding", @ -];7 [db rollback];8[DB executeupdate:@"INSERT into T_person (name, age) VALUES (?,?);",@"wendingding", @ -];9 [DB commit];Ten}];

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", @ A];3[DB executeupdate:@"INSERT into T_person (name, age) VALUES (?,?);",@"wendingding", @ at];4[DB executeupdate:@"INSERT into T_person (name, age) VALUES (?,?);",@"wendingding", @ -];5}];

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

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.