iOS Development database Chapter-fmdb Database queue (bottom)

Source: Internet
Author: User

iOS Development database Chapter-fmdb Database queue (bottom)

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//89#import"YYViewController.h"10#import"FMDB.h"1112@interfaceYyviewcontroller ()@property (nonatomic,strong) Fmdatabasequeue *Queue14@end1516@implementationYyviewcontroller1718-(void) Viewdidload19{20[Super Viewdidload];21st22//1. Get the path to the database fileNSString *doc=[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];NSString *filename=[doc stringbyappendingpathcomponent:@"Person.sqlite"];2526//2. Get the database queueFmdatabasequeue *queue=[Fmdatabasequeue Databasequeuewithpath:filename];28//Fmdatabase *db=[fmdatabase Databasewithpath:filename];2930//3. Open the 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);"];33If(Result) {NSLog (@"Success in the creation of a table");35}Else36{Panax Notoginseng NSLog (@"Failed to create a table");38}39}];Self.queue=Queue4142}4344-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event45{46//Inserting data47//[Self.queue indatabase:^ (Fmdatabase *db) {48//[DB executeupdate:@ INSERT into T_person (name, age) VALUES (?,?); ", @" wendingding ", @22];49//}];5051//Querying data[Self.queue indatabase:^ (Fmdatabase *DB) {53//1. Execute the query statementFmresultset *resultset = [db executeQuery:@"SELECT * from T_person"];5556//2. Traverse the results57While([ResultSet next]) {58int ID = [ResultSet intforcolumn:@"Id"];NSString *name = [ResultSet stringforcolumn:@"Name ""; 60 int age = [ResultSet intforcolumn:@ "age" ];61 NSLog (@ "%d%@%d, ID, name, age); 62 }63  }]; 64 65 }66 67  @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 : Examples of Banks Zhang San and John Doe accounts have 1000 yuan, if Zhang three to transfer to John Doe, need to execute two SQL statements, considering the security, the two fish can either be executed 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//Inserting data2 [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 executeupdate:@ " INSERT into T_person (name, age) VALUES (?,?); @" wendingding ", @25"; 8  [db commit];                

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", @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";  [db rollback]; 8 [db Executeupdate:@ "insert into T_person (name, age) VALUES (?, ?); @" wendingding ", @25";  [db commit];                

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", @223 [db executeupdate:@ " INSERT into T_person (name, age) VALUES (?,?); @" wendingding ", @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 (bottom)

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.