1. Get the path of the database file self. path = nshomedirectory (); self. path = [path stringbyappendingpathcomponent: @ "Documents/data. DB "]; 2. Get the database object and open the database. If the database does not exist, the fmdatabase * DB = [fmdatabase databasewithpath: path] will be automatically created. bool res = [dB open]; If (RES = No) {nslog (@ "failed to open"); return ;} else {nslog (@ "database opened successfully") ;}3, // If the table does not exist, create res = [dB executeupdate: @ "create table if not exists students (name, score, image) "]; // execute the SQL statement if (Re S = No) {nslog (@ "creation failed"); [dB close]; // close the database return;} else if (RES = Yes) {nslog (@ "created successfully");} 4. After the table is created, add, delete, modify, and query the table. 1> insert data res = [dB executeupdate: @ "insert into students values (?,?,?) ", Name, num, headdata]; If (RES = No) {nslog (@" insertion failed ");} [dB close]; 2>, delete operation: -(void) del :( ID) sender {fmdatabase * DB = [fmdatabase databasewithpath: path]; // open the database bool res = [dB open]; // if it fails, exit method if (RES = No) {nslog (@ "Open failed"); return;} // Delete res = [dB executeupdate: @ "delete from students where name =? ", Namefield. text]; If (RES = No) {nslog (@ "delete failed") ;}[ dB close] ;}3>, update operation:-(void) update :( ID) sender {fmdatabase * DB = [fmdatabase databasewithpath: path]; bool res = [dB open]; If (RES = No) {nslog (@ "failed to open"); return;} nsnumber * num = [nsnumber numberwithint: [scorefield. text intvalue]; Res = [dB executeupdate: @ "Update students set score =? Where name =? ", Num, namefield. text]; If (RES = No) {nslog (@ "failed to modify");} [dB close];} 4>, query operation:-(void) fetch :( ID) sender {fmdatabase * DB = [fmdatabase databasewithpath: path]; bool res = [dB open]; If (RES = No) {nslog (@ "failed to open"); return;} fmresultset * Set = [dB executequery: @ "select * from students"]; // fmresultset is equivalent to the cursor set // creates an array and stores all student information. nsmutablearray * array = [nsmutablearray arraywithcapacity: 0]; // traverses the students table while ([set next]) {// if there is another one, retrieve its data and close the database // name nsstring * name = [set stringforcolumn: @ "name"]; // score int score = [set intforcolumnindex: 1]; // photo nsdata * Data = [set dataforcolumnindex: 2]; // float a = [[set objectforcolumnindex: 3] floatvalue]; uiimage * image = [uiimage imagewithdata: Data]; studentitem * student = [[studentitem alloc] init]; student. name = Name; student. score = score; student. image = image; [array addobject: Student]; [STUDENT release];} [dB close];}