Use of Fmdb

Source: Internet
Author: User

  viewcontroller.m//  sqllite////  created by wup on 15/ 5/26.//  copyright  (c)  2015 year  apple. all rights reserved.//#import   "ViewController.h" #import  <sqlite3.h> #import   "Shop.h" #import   "FMDB.h" @interface  ViewController  ()  <UITableViewDataSource,UISearchBarDelegate> @property   (nonatomic, Strong)    NSMutableArray *shops; @property   (nonatomic,strong)  uitableview * tbv ; @property   (nonatomic,strong)   FMDatabase *fmdb; @end @implementation  viewcontroller-(nsmutablearray *) shops{    if  (!_shops)     {          _shops =[nsmutablearray array];//initializing arrays      }    return _shops;} -  (void) viewdidload {    [super viewdidload];    _tbv = [[uitableview alloc] initwithframe:self.view.bounds];     [self.view addSubview:_tbv];    _tbv.dataSource =  self;    _tbv.separatorstyle = uitableviewcellseparatorstylenone;// Remove the tableview above the horizontal line     self.tbv.contentinset = uiedgeinsetsmake (64, 0, 0  , 0 );     uisearchbar *uscb = [[uisearchbar alloc]  initwithframe:cgrectmake (0, 20, 320, 44)];    [self.view  addsubview:uscb];    uscb.delegate = self;    nsstring  *filename = [nssearchpathfordirectoriesindomains (nsdocumentationdirectory                                                                 ,  Nsuserdomainmask, yes). lastobject stringbyappendingstring:@ "Wup4.sqllite"] ;         _fmdb = [[fmdatabase alloc] initwithpath:filename];// Initialize sqlite    [_fmdb open];//Open database//    build table     [_fmdb  executeupdate:@ "create table if not exists t_shop  (id integer  Primary key, name text not null, price real); "];     /**     *   Data Sheet Insert data method      * /     for  (int i = 0 ;i < 100 ; i  ++)      {          nsstring *str = [nsstring stringwithformat:@ "Cell phone%d",i];          [_fmdb executeupdatewithformat:@ "INSERT INTO t_ Shop (Name, price)  VALUES  (%@, %u); ", Str,arc4random ()  % 1000];      }    /**     *   Data Sheet retrieval, The while loop transforms the data into a model and adds it to the array each time it gets to the next row of data.      *  fmres.next returns the type of bool, gets to TRUE, retrieves the completion that is false.      */    FMResultSet *fmres  = [_fmdb  executequery:@ "select name,price from t_shop;"];/ /database Query command     while  (fmres.next)  {         shop *testshop = [[shop alloc] init];         testshop.name  =  [fmres stringforcolumn:@ "Name"];        testshop.price =  [fmres stringforcolumn:@ "Price "];                    [self.shops addObject:testshop];             }    nslog (@ "%d", _shops.count),     nslog (@ "Retain  Count is %ld ",  cfgetretaincount ((__bridge cftyperef) _shops);//arc print retain count//     [_fmdb executeupdate:@ "delete from t_shop;"];    }; #pragma  mark - searchbar  proxy Method-(void) Searchbar: (uisearchbar *) Searchbar textdidchange: (nsstring *) searchtext{    nsstring *sql   = [nsstring stringwithformat:@ "select name,price from t_shop where  name like  '%%%@%% '  or price like  '%%%@%% '; ", searchtext,searchtext];        fmresultset * fmres  = [self.fmdb executequery:sql];//database Query Command          nslog (@ "%d", _shops.count),     nslog (@ "Retain count is %ld",  Cfgetretaincount ((__bridge cftyperef) _shops))//arc print Retain count    self.shops  = nil;    while  (Fmres.next)  {         shop *testshop = [[shop alloc] init];         testshop.name  =  [fmres stringforcolumn:@ "Name"];         testshop.price =  [fmres stringforcolumn:@ "Price"];                          [self.shops addobject:testshop];            }         nslog (@ "Retain count is %ld",  Cfgetretaincount ((__bridge cftyperef) _shops))//arc print RETAIN&NBSP;COUNT&NBSP;&NBSP;&NBSP;&NBSP;[SELF.TBV  reloaddata];} Proxy method for #pragma  mark - tableview -  (Nsinteger) TableView: (uitableview *) TableView  numberofrowsinsection: (Nsinteger) section{    return self.shops.count;     }-  (uitableviewcell *) TableView: (uitableview *) tableview  Cellforrowatindexpath: (nsindexpath *) Indexpath{    static nsstring *id  = @ "Cell";         uitableviewcell *cell   = [tableView dequeueReusableCellWithIdentifier:ID];    if  (cell  == nil)  {          cell  = [[uitableviewcell alloc] initwithstyle: uitableviewcellstylesubtitle reuseidentifier:id];    }         shop *test2shop = self.shops[indexPath.row];     cell.textlabel.text = test2shop.name;    cell.detailtextlabel.text    = test2shop.price;                 return cell;} @end

Use of Fmdb

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.