iOS Fmdb initial experience enhancement and deletion check

Source: Internet
Author: User

I haven't seen you for a long time. Miss you guys. Write an article on the use of Fmdb today. At work, we sometimes use a database, so you'll need a handy tool. Long story short begins to get to the point.

Import the Fmdb library file first. and join Libsqlite3.dylib. After these 2 preparations are ready, we can start working.

#import "ViewController.h" #import "FMDB.h"//import Header file @interface Viewcontroller () @property (Nonatomic,retain) Fmdatabasequeue *fmqueue;//Add a queue property @end@implementation viewcontroller//Your database path-(Fmdatabasequeue *) fmQueue{if ( _fmqueue== nil) {nsstring *dbpath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents/student.sqlite"]                ;                _fmqueue = [Fmdatabasequeue Databasequeuewithpath:dbpath];    NSLog (@ "%@", DbPath);    } return _fmqueue;      }-(void) viewdidload {[Super viewdidload];        [Self createallbtn];  This is in a code block [Self.fmqueue indatabase:^ (Fmdatabase *db) {//Creating a table [DB executeupdate:@ "CREATE table if not        exists t_student (ID integer primary key autoincrement, name text NOT NULL, age integer) "]; Keep the database open if (![        DB Open]) {return;    }    }];        }-(void) createallbtn{UIButton *btn = [UIButton Buttonwithtype:uibuttontypesystem]; Btn.frame = CGREctmake (100, 50, 100, 50);        [BTN settitle:@ "Add Data" forstate:uicontrolstatenormal];    [btn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];    [Btn addtarget:self Action: @selector (increaseaction:) forcontrolevents:uicontroleventtouchupinside];                [Self.view ADDSUBVIEW:BTN];        UIButton *btn1 = [UIButton Buttonwithtype:uibuttontypesystem];    Btn1.frame = CGRectMake (100, 100, 100, 50);        [btn1 settitle:@ "delete data" forstate:uicontrolstatenormal];    [Btn1 Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];    [Btn1 addtarget:self Action: @selector (deleteaction:) forcontrolevents:uicontroleventtouchupinside];                    [Self.view ADDSUBVIEW:BTN1];        UIButton *btn2 = [UIButton Buttonwithtype:uibuttontypesystem];    Btn2.frame = CGRectMake (100, 150, 100, 50);        [BTN2 settitle:@ "Modify data" forstate:uicontrolstatenormal];    [Btn2 Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal]; [Btn2 addtarget:self Action: @selEctor (modifyaction:) forcontrolevents:uicontroleventtouchupinside];                    [Self.view ADDSUBVIEW:BTN2];        UIButton *btn3 = [UIButton Buttonwithtype:uibuttontypesystem];    Btn3.frame = CGRectMake (100, 200, 100, 50);        [Btn3 settitle:@ "query data" forstate:uicontrolstatenormal];    [Btn3 Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];    [Btn3 addtarget:self Action: @selector (searchaction:) forcontrolevents:uicontroleventtouchupinside];    [Self.view Addsubview:btn3];            }-(void) Increaseaction: (UIButton *) btn{NSLog (@ "Add data"); [Self.fmqueue indatabase:^ (Fmdatabase *db) {for (int i = 0; i<100; i++) {nsstring *name = [NSString STRINGWITHF                        ormat:@ "James"];                                                int age = 10;                    [DB executeupdate:@ insert INTO t_student (name,age) VALUES (?,?) ", Name,[nsnumber Numberwithint:age]];    }                    }]; }-(void) Deleteaction: (UIButton *) btn{NSLog (@ "delete data"); [Self.fmqueue indatabase:^ (Fmdatabase *db)                {NSString *name = [NSString stringwithformat:@ "James"];        Note that this is not possible to delete the specified data, must be string nsstring type//[db executeupdate:@ "delete from t_student where name = '%@ '", name];        So nsstring *sql = [NSString stringwithformat:@ "delete from t_student where name = '%@ '", name];            [DB Executeupdate:sql];        }];            }-(void) Modifyaction: (UIButton *) btn{NSLog (@ "Modify data"); [Self.fmqueue indatabase:^ (Fmdatabase *db)                {NSString *name = [NSString stringwithformat:@ "Wq"];        NSString *set = [NSString stringwithformat:@ "UPDATE t_student set name = '%@ '", name];            [DB Executeupdate:set];            }];    }-(void) SearchAction: (UIButton *) btn{NSLog (@ "query data"); [Self.fmqueue indatabase:^ (Fmdatabase *db) {NSString *sql = [NSString stringwithformat:@ "Select *from T_studenT "];        Fmresultset *set = [db Executequery:sql];        while ([Set next]) {int stuid = [set intforcolumn:@ "id"];        NSString *name = [set stringforcolumn:@ "name"];                int age = [Set intforcolumn:@ ' age '];                NSLog (@ "Stuid =%d, name =%@, age =%d", Stuid, name, age);        Here the TextView is used to display the queried data uitextview *text = [[Uitextview alloc]init];        Text.frame = CGRectMake (50, 300, 300, 200);                        Text.backgroundcolor = [Uicolor Graycolor];        [Text Settext:name];                 [Self.view Addsubview:text];    }            }]; } @end

Yes, that's it, it's simple, you can pull it down to work.



iOS Fmdb initial experience enhancement and deletion check

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.