Simple use of IOS third-party database fmdatabase

Source: Internet
Author: User
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];}
 
 
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.