IOS数据库FMDB增、删、改、查的使用【原创】

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

IOS数据库FMDB增、删、改、查的使用[原创]

FMDB是一个XCODE的中一个轻量级的数据库,用于将网络资源存储在本地。所以,FMDB是一个很实用,很关键的知识点。在这里写了个简单的例子,基于FMDB的添删改查操作,代码可能比较乱,希望不要伤了各位的眼睛。其中添加删除更改的操作都非常简单,不需要做太多操作,只需要用到FMDB封装好的executeUpdate方法就行了。

第一步、加入sqlite3的包文件

40FD967B-8C57-4B3D-8357-E4D7235937CE

      如图所示,依次选择并查找工程名->build phases->link binary with libraries按下方的加号键,在弹出的搜索框中搜索sqlite3,选择libsqlite3.dylib->add。至此,已经添加完毕。然后在pod 中导入FMDB这个包。

第二步、增、删、改、查 的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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];
     }
}

至此,增删改查已经全部实现完毕,可能看的不是很懂我其中的的一些变量,其实变量都是次要的,真正操作增删改查的就那几个关键字,像插入中的 @”INSERT INTO ‘%@’ (‘%@’, ‘%@’, ….) VALUES (‘%@’, ‘%@’,…)”,第一个%@代表着表名,括号中的多个%@代表的表中元素的名字,后面VALUES中的多个%@都是一一对应前面的元素的值。好了,介绍就到这里,感谢查看。

原文地址:https://www.cnblogs.com/zxykit/p/6164038.html