使用FMDB对数据库实现增删查改

需要的头文件和类库

import

import “FMDB.h”

libsqlite3.0.tdb(Xcode7.0之后dylib变为tdb)

// 获取数据库路径 ,有数据库就打开,没有就创建

  NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *dbPath = [documentsPath stringByAppendingPathComponent:@"Student.sqlite"];
    NSLog(@"%@",dbPath);

// 得到数据库路径后初始化FMDB(声明个属性方便使用 @property (nonatomic, strong) FMDatabase * database;)

 self.database = [FMDatabase databaseWithPath:dbPath];

// 创建表

 [self.database open];
    // executeUpdate:@"create table 表名 (列名 类型,..... )"   对数据库操作表名要保持一致
    [self.database executeUpdate:@"create table user (name text, age integer, myData blob)"]; 
    [self.database close];

// 用FMDB对数据库操作就是FMDB的对象调用方法再加常用的SQLite语句

// 增加数据 FMDB方法:

- (IBAction)insert:(id)sender {
    [self.database open];
    // 写一个数组
    NSMutableArray * array = [[NSMutableArray alloc] init];
    [oneArr addObject:@"111"];
    [oneArr addObject:@"222"];
   // 把数组转为data
    NSData * arrayData = [NSKeyedArchiver archivedDataWithRootObject:array];
    // FMDB方法:executeQuery
    // SQLite语法:insert into 表名(列名) values(?)
    FMResultSet *rs =  [self.database executeQuery:@"insert into user(name,age,myData) values(?,?,?)" withArgumentsInArray:@[@"张三",@18,arrayData]];
    或者写成FMResultSet *rs =  [self.database executeQuery:@"insert into user(name,age,myData) values(?,?,?)", @"张三", @18, arrayData];
    // 把数据循环加入到数据库中
    [rs next];
    [self.database close];
}

// 修改数据

- (IBAction)update:(id)sender {
   [self.database open];
    NSString * newName = @"李四";
    NSString * oldName = @"张三";
    // FMDB方法:executeUpdateWithFormat
    //SQLite语法:update 表名 set 新的参数 = 类型 where 旧的参数 = 类型
    [self.database executeUpdateWithFormat:@"update user set name = %@ where name = %@",newName,oldName];
    NSLog(@"修改数据成功");
    [self.database close];
}

// 删除数据

- (IBAction)delete:(id)sender {
    [self.database open];
    // 删除所有
    // FMDB方法:executeUpdateWithFormat
    // SQLite语法: delete from 表名
    [self.database executeUpdateWithFormat:@"delete from user"];

    // 按条件删除
    // FMDB方法:executeUpdateWithFormat
    // SQLite语法: delete from 表名 where 条件
    // NSString * name = @"张三";
    // [self.database executeUpdateWithFormat:@"delete from user where name = %@",name];
    [self.database close]; 
}

// 查询

- (IBAction)select:(id)sender {
    [self.database open];
    //    按条件查询
    //    FMDB方法:intForQuery(根据返回值类型来写)
    //    SQLite语法: select 要返回的列名 from 数据库名 where 查询的列名 = ?",@"查询条件"
//        NSInteger age = [self.database intForQuery:@"select age from user where name = ?",@"张三"];

    //查询整个表
    //    FMDB方法:executeQuery
    //    SQLite语法: select * from 表名
    FMResultSet * resultSet = [self.database executeQuery:@"select * from user"];
    //[resultSet next]遍历条件 遍历前为YES 遍历结束为NO
    while ([resultSet next] == YES)
    {
//         用[resultSet stringForColumn:]或[resultSet intForColumn:]等方法把值转成Object-C的型态
        NSString * name = [resultSet stringForColumn:@"name"];
        NSInteger  age = [resultSet longLongIntForColumn:@"age"];
        NSData *data = [resultSet dataForColumn:@"myData"];
        //data转为数组
        NSMutableArray *testArr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        // 打印数据
        NSLog(@"name = %@, age = %ld,myData = %@",name, age, testArr);
    }
    [self.database close];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值