FMDB取出所有数据

该博客详细介绍了如何在iOS应用中使用FMDB库来打开并创建SQLite数据库,以及如何查询数据库中的所有数据。在`viewDidLoad`方法中,首先确定数据库路径,接着创建并打开数据库。如果打开成功,进一步创建数据库表。然后定义了一个`loadData`方法,通过执行SQL查询获取`t_makeup`表中的所有列,并将结果映射到`makeupModel`对象上,最后更新表格视图展示数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
@interface DBfaceCleanserViewController ()<AddviewControllerDelegate,EditViewControllerDelegate>

@property (nonatomic,strong) FMDatabase *db;
@property (nonatomic,strong) NSMutableArray *makeup;

@end

@implementation DBfaceCleanserViewController


- (void)viewDidLoad {
    [super viewDidLoad];

 
     NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    // 拼接文件名
    NSString *filePath = [cachePath stringByAppendingPathComponent:@"makeup.sqlite"];
    // 创建一个数据库的实例,仅仅在创建一个实例,并会打开数据库
    FMDatabase *db = [FMDatabase databaseWithPath:filePath];
    _db = db;
    // 打开数据库
    BOOL flag = [db open];
    if (flag) {
        NSLog(@"打开成功");
    }else{
        NSLog(@"打开失败");
    }
    
    // 创建数据库表
    // 数据库操作:插入,更新,删除都属于update
    // 参数:sqlite语句
    BOOL flag1 = [db executeUpdate:@"create table if not exists t_makeup (id integer primary key autoincrement,productName text, productBrand text, productDate text, productExpiration text, productPrice text, productTip text ,productImage blob);"];
    if (flag1) {
        NSLog(@"创建成功");
    }else{
        NSLog(@"创建失败");
        
    }

    
    _makeup = [[NSMutableArray alloc] init];
    [self loadData];
    
 }


-(void)dealloc
{
    [_db close];
}

- (void)loadData
{
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//        NSLog(@"全部");
//        [self.makeup addObjectsFromArray:[makeupModel findAll]];
    
    
    FMResultSet *result =  [_db executeQuery:@"select * from t_makeup"];
    
    // 从结果集里面往下找
    while ([result next]) {
        
       <span style="color:#FF0000;"><strong> makeupModel *makeup = [[makeupModel alloc] init];
        makeup.productName = [result stringForColumn:@"productName"];
        makeup.productBrand = [result stringForColumn:@"productBrand"];
        makeup.productPrice = [result stringForColumn:@"productPrice"];
        makeup.productDate = [result stringForColumn:@"productDate"];
        makeup.productExpirationDate = [result stringForColumn:@"productExpiration"];
        makeup.productTip = [result stringForColumn:@"productTip"];
        makeup.productImage = [result dataForColumn:@"productImage"];</strong></span>

        <span style="color:#FF0000;">[self.makeup addObject:makeup];
    }</span>
    
    
    

    
    
        [self.tableView reloadData];
    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值