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

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

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

第一步、加入sqlite3的包文件

如图所示,依次选择并查找工程名->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中的多个%@都是一一对应前面的元素的值。好了,介绍就到这里,感谢查看。

最新文章

  1. SQL Server 2008 R2 升级到 Service Pack 3后Report Builder启动不了
  2. owin建控制台应用程序步骤
  3. C++ new失败的处理
  4. 7.python字符串-内置方法分析
  5. mysql error: Access denied for user 'root'@'localhost' (using password: YES)
  6. 小谈@override
  7. boost function对象
  8. EXEC 和 SP_EXECUTESQL的区别
  9. webapp之路--之query media
  10. 单片机课程设计——《基于AT89S52单片机和DS1302时钟芯片的电子时钟(可蓝牙校准)》
  11. 递归的二叉查找树Java实现
  12. HA集群heartbeat配置--Nginx
  13. KindEditor富文本编辑器, 从客户端中检测到有潜在危险的 Request.Form 值
  14. SpringBoot前端模板
  15. [学习笔记]修改关键跳无效且关键CALL又不存在的情况
  16. MySQL使用select查询时,在查询结果中增加一个字段并指定固定值
  17. jenkins中通过execute shell启动的进程会被杀死的问题
  18. selenium及webdriver的原理【转】
  19. ubuntu16.04系统安装
  20. TouchID 指纹解锁

热门文章

  1. Python作用域
  2. JAVA面向对象3---多态
  3. 项目中的BaseServlet
  4. 深入Java单例模式
  5. HDU1879--继续畅通工程(最小生成树)
  6. java 接口参数
  7. 转:用​C​语​言​的​r​a​n​d​(​)​和​s​r​a​n​d​(​)​产​生​伪​随​机​数​的​方​法​总​结
  8. Struts2 语法--result type
  9. 用telnet命令,SMTP发送邮件
  10. 很好的一个dp题目 Codeforces Round #326 (Div. 2) D dp