What is Fmdb?
Fmdb is the SQLite database framework for the iOS platform
Fmdb encapsulates the C language API of SQLite in OC mode
What are the advantages of Fmdb?
Use more object-oriented, save a lot of cumbersome, redundant code
Compare the CoreData frame with Apple's own, more lightweight and flexible
Provides a multi-threaded secure database operation method, effective local records data confusion.
Fmdb's GitHub escrow address: Https://github.com/ccgus/fmdb
Fmdatebase object: Represents a standalone SQLite database
Open/Create a database:
Fmdatebase *db = [Fmdatabase databasewithpath:path];if (![ DB Open] {NSLog (@ "Database open failed!) ");}
Three description of the file path:
A. Specific file name: If it does not exist, it will create
B. Empty string @ "": Temp directory creates an empty, delete when Fmdatabase connection is closed
C. A temporary database is created in memory and deleted when the Fmdatabase connection is closed
Database shutdown
-(BOOL) Close
Fmdatabase This class is thread insecure, and if you use a Fmdatabase instance in multiple threads at the same time, it can cause data clutter.
To ensure thread safety, Fmdb provides a quick and easy Fmdatabasequeue class.
The creation of the Fmdatabasequeue object:
+ (Instancetype) Databasequeuewithpath: (NSString *) Apath
The Fmdatabase object is encapsulated in it and opened at the same time as it was created.
Shut down:
-(void) Close
Simply put, the Fmdatabasequeue Fmdatabase is packaged and recommended for use with Fmdatabasequeue
Such as:
-(void) setupfmdb{NSString * Path = [NSString stringwithformat:@ "%@/documents/h.sqlite", Nshomedirectory ()]; Self.queue = [Fmdatabasequeue databasequeuewithpath:path];}
Fmdatabasequeue is not a fmdatabase, and there is no way to execute SQL statements directly
Use the Indatabase method to pass in the code that executes the SQL statement by using block mode
-(void) Indatabase: (void (^) (Fmdatabase *db)) block
The parameter db of the block is the Fmdatabase object encapsulated inside the Fmdatabasequeue
In Fmdb, all operations except queries are "updated", such as: Create drop insert Delete update
Use the Executeupdate: method to perform the update, which passes an SQL statement string and can specify the format parameter
-(BOOL) Executeupdate: (NSString *) SQL, ...-(BOOL) Executeupdate: (NSString *) SQL Withargumentsinarray: (Nsarray *) arguments-(BOOL) executeupdate: (NSString *) SQL Witherrorandbindings: (nserror *) Outerr, ...-(BOOL) Executeupdate: ( NSString *) SQL Withparameterdictionary: (nsdictionary *) arguments-(BOOL) executeupdate: (NSString *) SQL Withvalist: (VA _list) args-(BOOL) Executeupdatewithformat: (NSString *) format, ...
This is a family function, with the same function, choose a calling method that you are accustomed to
Such as:
-(void) Updatedesc: (nsstring*) desc Forid: (nsstring*) id{[Self.queue indatabase:^ (Fmdatabase *db) {if (![ DB executeupdate:@ "Update T_hero set desc=? Where id=? ", desc, id]) {NSLog (@"%@ ", [db lasterrormessage]); } }];}
Executes a query statement using the method Exectequery:, which passes a query SQL statement, and can specify a placeholder parameter that returns a query result set object
-(Fmresultset *) ExecuteQuery: (NSString *) SQL, ...-(Fmresultset *) ExecuteQuery: (NSString *) SQL Withargumentsinarray: ( Nsarray *) arguments-(Fmresultset *) ExecuteQuery: (NSString *) SQL Withparameterdictionary: (nsdictionary *) arguments-( Fmresultset *) Executequerywithformat: (NSString *) format, ...-(Fmresultset *) ExecuteQuery: (NSString *) SQL Withvalist :(va_list) args
The Fmresultset object is the result set object, which is the chain table structure and iterates through the results of each row through next.
You can either pass the field value through the subscript of the column, or you can get the field value from the field name
Such as:
-(nsstring*) Getdescwithid: (nsstring*) id{__block nsstring *pret = nil; [Self.queue indatabase:^ (Fmdatabase *db) {Fmresultset * rs = [db executequery:@ "select desc from T_hero where id=?;", id]; if ([Rs next]) {pret = [rs stringforcolumn:@ "desc"]; } [Rs Close]; }]; return pret;}
What is a transaction?
Also known as a unit of work, is a sequence of one or more SQL statements, as a complete unit of work
such as: A to Bank for b transfer 1000 Yuan
Action 1: A account-1000
Operation 2:b Account + 1000
Two operations are put together to form a completed unit of work.
Why transaction processing?
If Operation 1 succeeds, but operation 2 fails, a * * * annoying
Handle two operations in one transaction to ensure simultaneous success or failure
SQL statements for transactions:
Start transaction BEGIN TRANSACTION
Commit TRANSACTION COMMIT TRANSACTION
Rolling back transactions ROLLBACK transaction
Fmdb transaction processing, such as:
[Self.queue intransaction:^ (void *db, BOOL * rollback) {Fmresultset * rs = [db executequery:@ "select * from T_hero;"]; while ([Rs next]) {//...} if (![ DB executeupdate:@ "INSERT into t_table (name) VALUES (:)" @ "Rose"]) {*rollback = YES; }}];
This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1746490
SQLite3 Object-oriented encapsulation Fmdb