iOS database Fmdb, delete, change, check the use of "original"

Source: Internet
Author: User

Http://blog.it985.com/13588.html

iOS database Fmdb Use of Add, delete, change, check [original]

Fmdb is a lightweight database in Xcode that is used to store network resources locally. So, Fmdb is a very practical, very key point of knowledge. Here to write a simple example, based on the addition of Fmdb to check the operation, the code may be messy, I hope you do not hurt the eyes. The addition of the deletion changes is very simple, do not need to do too much, just need to use the Fmdb encapsulated Executeupdate method is OK.

The first step, add Sqlite3 package file

, select and find the project name->build Phases->link binary with libraries press the plus key below, search for sqlite3 in the pop-up search box, select Libsqlite3.dylib->add. At this point, you have added the complete. Then import the Fmdb package into the pod.

The realization of the second step, increment, delete, change and check
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869 #pragma mark - FMDB数据库操作//插入-(void)insert{  if ([_db open]) {     NSString *sql1 = [NSString stringWithFormat:                       @"INSERT INTO ‘%@‘ (‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘) VALUES (‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘)",                       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];    }}//修改-(void)update{    if ([_db open]) {     NSString *updateSql = [NSString stringWithFormat:@"update %@ set %@=‘%@‘ where %@=‘%@‘",TABLENAME,DESTINATION,@"目的地 上海",STARTING,@"芜湖 出发地"];        BOOL res = [_db executeUpdate:updateSql];        if (!res) {            NSLog(@"error when insert db table");        } else {            NSLog(@"success to insert db table");        }        [_db close];    }}//删除-(void)delete{     if ([_db open]) {         NSString *deleteSql = [NSString stringWithFormat:                                 @"delete from %@ where %@ = ‘%@‘",                                 TABLENAME, STARTING, @"芜湖 出发地"];         BOOL res = [_db executeUpdate:deleteSql];         if (!res) {             NSLog(@"error when insert db table");         } else {             NSLog(@"success to insert db table");         }         [_db close];     }} //查询 - (void)query {     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];     }}

At this point, add and remove changes have been fully realized, may not be able to see some of my variables, in fact, the variables are secondary, the real operation of adding and deleting the key words, such as insert in the @ "insert INTO '%@ ' ('%@ ', '%@ ', ...) VALUES ('%@ ’, ‘%@’,...)” , the first%@ represents the name of the table, multiple%@ in parentheses represent the names of the elements in the table, and many of the%@ in the following values are one by one corresponding to the previous element. Well, the introduction is here, thanks for checking it out.

iOS database Fmdb, delete, change, check the use of "original"

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.