Development of iOS database Fmdb, delete, change, check the use of the tutorial

Source: Internet
Author: User


Fmdb is a lightweight database in a Xcode that is used to store network resources locally. Therefore, Fmdb is a very practical, very important point of knowledge. Here is a simple example, based on the fmdb of the TIM-censored operations, the code may be more messy, I hope not to hurt your eyes. It is very simple to add delete changes, do not need to do too many operations, only need to use the Fmdb encapsulation of the good executeupdate method on the line.

First step, add the Sqlite3 package file


As shown in the figure, select and find the engineering name->build Phases->link binary with libraries press the plus key below, search the pop-up search box for sqlite3, select libsqlite3.dylib-> Add At this point, you have finished adding. Then import Fmdb this package in pod.

The second step, increase, delete, change, check the realization

#pragma  mark - fmdb database Operations//inserts-(void) Insert {  if  ([_db open])  {      nsstring *sql1 = [nsstring stringwithformat:                          @ "insert into  '%@ '   ('%@ ',  '%@ ',  ', '%@ ',  '%@ ' ',  ',"%@ ',  '%@ '),
nbsp
'%@ ',  '%@ ',  '%@ ',  '%@ ')  VALUES  ('%@ ',  '%@ ',  ',%@ ',  '%@ ',  ', '%@ ' ,  '%@ ',  '%@ ',  '%@ ',  '%@ ',  ' "%@ ')",         &nbs p;              tablename, @ " Starting ",  @" destination ",  @" Goodsname ",  @" car ",  @" Goodsweight ",  @" Goodsvolume ", @" Intentionprice ", @" bail ", @" Mark ", @" status ", _startinglabel.text,_destinationlabel.text,gOodsnametextfield.text,selectvehiclelabel.text,goodsweighttextfield.text,goodsvolumetextfield.text,

intentionpricetextfield.text,bailtextfield.text,marklabel.text,@ "0"];

     BOOL res = [_db executeUpdate:sql1];        if  (!res)  {        

 nslog (@ "error when insert db table");      } else {         nslog (@ "

Success to insert db table ");

     }      [_db close];    &NBSP}}//Modify-(void) Update {    if  ([_db open])  { &nbs p;   nsstring *updatesql = [nsstring stringwithformat:@ "update %@  set %@= '%@ '  where %@= '%@ ', tablename,destination, @ "destination   Shanghai", Starting,@ "Wuhu   Departure"];

        BOOL res = [_db executeUpdate:updateSql];           if  (!res)  {     

       nslog (@ "error when insert db table");         } else {       

     nslog (@ "success to insert db table");         }         [_db 

Close];    &NBSP}  }//delete-(void) Delete {     if  ([_db open]) &nbsp ; {         NSString *deleteSql = [NSString  stringWithFormat:                                  @ " delete from %@ where %@ =  '%@ ',                            

       tablename, starting, @ "Wuhu   Departure"];          bool res = [_db executeupdate:

Deletesql];            if  (!res)  {              nslog (@ "error when insert db table")

;          } else {      

       nslog (@ "success to insert db table");          }          [_db close];     &NBSP}}    //query  -  (void) query  {     &NBSP ; if  ([_db open])  {         nsstring *  sql = [nsstring stringwithformat:                             @ "

select * from %@ ", TableName];          FMResultSet * rs = [_db 

Executequery:sql];          while  ([Rs next])  {              int id = [rs intforcolumn:@ "Id

"];              nsstring * name = [rs stringforcolumn:starting];              nsstring * age =

 [rs stringForColumn:DESTINATION];              nsstring * address

 = [rs stringForColumn:GOODSNAME];              nslog (@ "id = %d,  name = %@, age = %@  address = %@ ",  Id, name, age,

 address);          }          [

_db close];     &NBSP}}


To this end, the change and deletion of the search has been completed, may not see very understand me some of the variables, in fact, the variables are secondary, real operation add and remove changes to check on those few keywords, like inserted in the @ "insert into the '%@ ' ('%@ ', '%@ ', ...) VALUES ( '%@ ', '%@ ',...) ', the first%@ represents the name of the table, the names of the elements in the table in parentheses, and multiple%@ in the following values are the values of one by one corresponding to the preceding elements. OK, so here's the introduction, thanks for viewing.

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.