First introduce Fmdb third party, click View Method
First, create a table
1. Creating SQL statements
NSString *createsql = @ "CREATE table if not exists t_student (ID integer primary key autoincrement not NULL, name text not NULL, age integer NOT null, sex text no null) ";
2. Locate the storage path
NSString *document = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) objectAtIndex : 0];
NSLog (@ "document =%@", document);
Self.filepath = [Document stringbyappendingpathcomponent:@ "Student.sqlite"];
NSLog (@ "FilePath =%@", Self.filepath);
3. Initializing a Fmdb object with a path
Self.database = [Fmdatabase DatabaseWithPath:self.filePath];
4. You need to determine when the database is open to execute the statement
if ([Self.database Open]) {
BOOL result = [Self.database executeupdate:createsql];
if (result) {
NSLog (@ "Build table success");
} else {
NSLog (@ "Build table failed");
}
}
5. Close the database
[Self.database Close];
Second, increase and revise the search
Open the database First: [Database open];
Set the corresponding SQL statement: NSString *sql = @ "corresponding SQL statement";
The database goes the corresponding method, obtains the result, Fmdb This result is the bool type, but is not the ordinary data in the int type . BOOL result = [Database executeupdate: Corresponding statement, (parameter)];
The results of the result are judged, success is output success, failure print error code
if (result) {
NSLog (@ "Insert success");
} else {
NSLog (@ "Insert failed, result =%d", result);
}
Close the database.
1. Add: INSERT into T_student (name, age, Sex) VALUES (?,?,?)
Add a second way, queue (optimization mode):
Fmdb does not support simultaneous operation of multiple threads, so it is generally implemented in a serial way
[Self.database Open]
First step: Create an action queue
Fmdatabasequeue *queue = [Fmdatabasequeue DatabaseQueueWithPath:self.filePath];
Identity: Record whether the operation was successful
__block BOOL issucceed = YES;
Step Two: Pack what you need to do and put it in the operation queue
[Queue intransaction:^ (Fmdatabase *db, BOOL *rollback) {
Serial queue
Issucceed = [db executeupdate:@ "insert into t_student (name, age, Sex) VALUES (?,?,?)", @ "Old King next Door", @38, @ "male" && IsS Ucceed;
Issucceed = [db executeupdate:@ "insert into t_student (name, age, Sex) VALUES (?,?,?)" @ "Zhao Fu One" @ "438" @ "male"] && I Ssucceed;
Issucceed = [db executeupdate:@ "insert into t_student (name, age, Sex) VALUES (?,?,?)", @ "AJAR", @ "18", @ "male" && I Ssucceed;
if (issucceed) {
NSLog (@ "Add multiple successes");
} else {//If there is an error
Block returns the parameter rollback for processing (bool type pointer)
*rollback = YES;
return;
}
}];
[Self.database Close];
2. Delete: Delete from t_student where name =? ", @" AAA "
3. Change: Update t_student set name =? WHERE name =? ", @" BBB ", @" CCC "
4. Check:select * from T_student
Class Fmresultset used for query results
Fmresultset *resultset = [Database executequery:@ "query statement"];
Simple use of iOS advanced _fmdb