On Thursday, Friday in the busy company's things and stocks, did not have time to update the blog, this week to fill, learning summary under Fmdb.
Fmdb is a package for sqlite, especially in the case of multithreading, using SQLite is very cumbersome, while using Fmdb is relatively simple, the following is a code example using Fmdatabase and Fmdatabasequeue
viewcontroller.m//fmdbdemo////Created by Cyw on 15-4-26.//Copyright (c) 2015 Cyw. All rights reserved.//#import "ViewController.h" #import "FMDB.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload{[super viewdidload];//Fmdb commonly used three classes://1.FMDatabase: Single SQLite database for executing SQL statements// 2.FMDatabaseQueue: Result set of SQL statements//3.FMRESULTSET:SQL statements executed under multithreading//1. Database file path NSString *path=[nssearchpathfordirectorie Sindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString *filepath=[path stringbyappendingpathcomponent:@ "Cyw.sqlite"]; NSLog (@ "%@", FilePath); Create Fmdb object//parameter has 3 in the case://1. File path file does not exist automatically created//2.nil creates a temporary database in memory and automatically destroys when fmdatabase shuts down//[email protected] "" will create an empty database in the temp directory, destroy when closed//fmdatabase *db=[[fmdatabase alloc]initwithpath:filepath];//fmdatabase *bd=[fmdatabase datab Asewithpath:filepath]; Open Database//if ([db Open]) {////New database//[self createtable:db];///////Pluginto data//[self insertdata:db];////query data//[self selectdata:db];//[db close];//}//database There are locks and things In Fmdb can be implemented with Fmdatabasequeue//lock mainly solves the problem of multi-threaded transactions is added an implicit lock//CREATE DATABASE queue Fmdatabasequeue *queue=[fmdatabasequeue databas equeuewithpath:filepath];//[Self queueselect:queue];//[self queuetransaction:queue]; [Self queueintranscation:queue]; }-(BOOL) CreateTable: (fmdatabase*) db{//This two SQL performs an error together//create table if not exists p_class (CId integer primary key, C Name varchar (20)); NSString *[email protected] "CREATE table if not exists person (ID integer primary key autoincrement, age integer Not n Ull,name text not Null,photo blob,registertime datetime DEFAULT (DateTime (Current_timestamp, ' localtime ')), sex char (1) Default ' 0 ', Money Numeric (10,2), classId integer,constraint fk_person_class foreign Key (classId) references P_class (CId ));";/ /fmdatabase update operations are all using the Executeupdate method query using the ExecuteQuery method BOOL result= [db executeupdate:strsql]; if (reSult) {NSLog (@ "Database creation succeeded"); } else {NSLog (@ "Database creation failed"); } return result; -(void) InsertData: (fmdatabase*) db{[db executeupdate:@ "delete from person"]; Different parameters of the Executeupdate method//[DB executeupdate:@ "INSERT into P_class (cid,cname) values (?,?);" withargumentsinarray:@[@ 1001,@ "Software Engineering"]; Note Add @ [db executeupdate:@ insert INTO person (NAME,AGE,MONEY,CLASSID) values (?,?,?,?); ", @" Cuiyw ", @24,@200.3,@1001]; [DB executeupdatewithformat:@ INSERT INTO person (NAME,AGE,MONEY,CLASSID) VALUES (%@,%d,%f,%d), @ "Cyw", 23,210.9,1001 ];} -(void) Selectdata: (fmdatabase*) db{//query data nsstring *[email protected] "Select Id,name,age,money from Person";// Fmresultset return result set Fmresultset *set=[db Executequery:strsql]; while ([Set next]) {int pid=[set intforcolumn:@ "id"]; NSString *name=[set Stringforcolumnindex:1]; int Age=[set intforcolumn:@ "age"]; float Money=[set Doubleforcolumnindex:3]; NSLog (@ "%d%@%d%f", Pid,name,age,moNey); } nsstring *[email protected] "Select Cid,cname from P_class"; Fmresultset return result set Fmresultset *set1=[db EXECUTEQUERY:STRSQL1]; while ([Set1 next]) {nsstring *name=[set1 stringforcolumnindex:1]; int Age=[set1 intforcolumn:@ "CId"];//float money=[set doubleforcolumnindex:2]; NSLog (@ "%@%d", name,age); }}-(void) Queueselect: (fmdatabasequeue*) queue{//Open Database [queue indatabase:^ (Fmdatabase *db) {[Self selectdata: DB]; }];} Two ways of writing a transaction-(void) Queuetransaction: (fmdatabasequeue*) queue{[Queue indatabase:^ (Fmdatabase *db) {[Self selectdata :d b]; [DB BeginTransaction]; [DB executeupdate:@ "update person set money=money+20.0 where id=4"]; [DB executeupdate:@ "update person set money=money-20.0 where id=3"]; [DB commit]; [Self selectdata:db]; }];} -(void) Queueintranscation: (fmdatabasequeue*) queue{[Queue intransaction:^ (Fmdatabase *db, BOOL *rollback) {[sel F SELECTDATA:DB]; [DB executeupdate:@ "update person set money=money+20.0 where id=4"]; [DB executeupdate:@ "update person set money=money-20.0 where id=3"]; [Self selectdata:db]; }];} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Third-party Fmdb of data storage