#import "FMDB.h"
@interface Czviewcontroller ()
-(ibaction) Insertonclick;
-(ibaction) Deleteonclick;
-(ibaction) Updateonclick;
-(ibaction) Queryonclick;
@property (nonatomic, strong) Fmdatabase *db;
@property (nonatomic, strong) Fmdatabasequeue *queue;
@end
@implementation Czviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
0. Get sandboxed addresses
NSString *path = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject];
NSString *sqlfilepath = [path stringbyappendingpathcomponent:@ "Student.sqlite"];
1. Create a Fmdatabasequeue object
Whenever you create a database queue object, Fmdb automatically loads the database objects inside
Self.queue = [Fmdatabasequeue Databasequeuewithpath:sqlfilepath];
2. Perform the action
will create a good database from the block delivery queue to us
[Self.queue indatabase:^ (Fmdatabase *db) {
Write code that needs to be executed
2.1 Creating tables (in the FMDB framework, Add/Remove/modify/Create/Destroy are collectively referred to as updates)
BOOL success = [db executeupdate:@ "CREATE TABLE IF not EXISTS t_student (id INTEGER PRIMARY KEY autoincrement, name TEXT N OT NULL, score REAL DEFAULT 1); "];
if (success) {
NSLog (@ "CREATE table succeeded");
}else
{
NSLog (@ "Failed to create TABLE");
}
}];
/*
1. Loading database objects
self.db = [Fmdatabase Databasewithpath:sqlfilepath];
2. Open the Database
if ([Self.db Open])
{
NSLog (@ "open successfully");
2.1 Creating tables (in the FMDB framework, Add/Remove/modify/Create/Destroy are collectively referred to as updates)
BOOL success = [Self.db executeupdate:@ "CREATE TABLE IF not EXISTS t_student (id INTEGER PRIMARY KEY autoincrement, name t EXT not NULL, score REAL DEFAULT 1); "];
if (success) {
NSLog (@ "CREATE table succeeded");
}else
{
NSLog (@ "Failed to create TABLE");
}
}else
{
NSLog (@ "Open failed");
}
*/
}
-(Ibaction) Insertonclick
{
/*
Fmdb can be used as placeholders, but note: If you use a question-mark placeholder, you can only pass objects to placeholders later
BOOL success = [Self.db executeupdate:@ "INSERT into T_student (score, name) VALUES (?,?);" @ (@), @ "Jack"];
if (success) {
NSLog (@ "Insert success");
}else
{
NSLog (@ "Insert failed");
}
*/
[Self.queue indatabase:^ (Fmdatabase *db) {
BOOL success = [db executeupdate:@ "INSERT into T_student (score, name) VALUES (?,?);" @ (@), @ "Jackson"];
if (success) {
NSLog (@ "Insert success");
}else
{
NSLog (@ "Insert failed");
}
}];
}
-(Ibaction) Deleteonclick
{
}
-(Ibaction) Updateonclick
{
/*
[Self.queue indatabase:^ (Fmdatabase *db) {
Open transaction
[DB BeginTransaction];
[DB executeupdate:@ "UPDATE t_student SET score =" WHERE name = ' ZS '; "];
Nsarray *array = @[@ "abc"];
ARRAY[1];
[DB executeupdate:@ "UPDATE t_student SET score = WHERE name = ' ls ';"];
Commit a transaction
[DB commit];
}];
*/
[Self.queue intransaction:^ (Fmdatabase *db, BOOL *rollback) {
[DB executeupdate:@ "UPDATE t_student SET score =" WHERE name = ' ZS '; "];
Nsarray *array = @[@ "abc"];
ARRAY[1];
[DB executeupdate:@ "UPDATE t_student SET score = WHERE name = ' ls ';"];
}];
}
-(Ibaction) Queryonclick
{
/*
ExecuteQuery method for querying in Fmdb framework
Fmresultset result set, the result set is actually like tablevivew.
Fmresultset *set = [self.db executequery:@ "SELECT ID, name, score from T_student;"];
while ([Set next]) {//Next method returns YES indicates data is desirable
int ID = [set intforcolumnindex:0];
NSString *name = [set stringforcolumnindex:1];
NSString *name = [set stringforcolumn:@ "name"]; Remove the corresponding value based on the field name
Double score = [Set doubleforcolumnindex:2];
NSLog (@ "%d%@%.1f", ID, name, score);
}
*/
[Self.queue indatabase:^ (Fmdatabase *db) {
Fmresultset result set, the result set is actually like tablevivew.
Fmresultset *set = [db executequery:@ "select ID, name, score from T_student;"];
while ([Set next]) {//Next method returns YES indicates data is desirable
int ID = [set intforcolumnindex:0];
NSString *name = [set stringforcolumnindex:1];
NSString *name = [set stringforcolumn:@ "name"]; Remove the corresponding value based on the field name
Double score = [Set doubleforcolumnindex:2];
NSLog (@ "%d%@%.1f", ID, name, score);
}
}];
}
@end
iOS Development Database Chapter---fmdatabasequeue Database queue security Operations