IOS-将任意对象存进数据库

model

 1 //
 2 //  HMShop.h
 3 //  将任意对象存进数据库
 4 //
 5 //  Created by apple on 14/11/20.
 6 //  Copyright (c) 2014年 heima. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface HMShop : NSObject <NSCoding>
12 @property (nonatomic, copy) NSString *name;
13 @property (nonatomic, assign) double  price;
14 
15 @end
16 
17 
18 //
19 //  HMShop.m
20 //  将任意对象存进数据库
21 //
22 //  Created by apple on 14/11/20.
23 //  Copyright (c) 2014年 heima. All rights reserved.
24 //
25 
26 #import "HMShop.h"
27 
28 @implementation HMShop
29 - (void)encodeWithCoder:(NSCoder *)encoder
30 {
31     [encoder encodeObject:self.name forKey:@"name"];
32     [encoder encodeDouble:self.price forKey:@"price"];
33 }
34 
35 - (id)initWithCoder:(NSCoder *)decoder
36 {
37     if (self = [super init]) {
38         self.name = [decoder decodeObjectForKey:@"name"];
39         self.price = [decoder decodeDoubleForKey:@"price"];
40     }
41     return self;
42 }
43 
44 - (NSString *)description
45 {
46     return [NSString stringWithFormat:@"%@ <-> %f", self.name, self.price];
47 }
48 @end

Controller

 1 //
 2 //  HMViewController.m
 3 //  将任意对象存进数据库
 4 //
 5 //  Created by apple on 14/11/20.
 6 //  Copyright (c) 2014年 heima. All rights reserved.
 7 //
 8 
 9 #import "HMViewController.h"
10 #import "HMShop.h"
11 #import "FMDB.h"
12 
13 @interface HMViewController ()
14 @property (nonatomic, strong) FMDatabase *db;
15 @end
16 
17 @implementation HMViewController
18 
19 - (void)viewDidLoad
20 {
21     [super viewDidLoad];
22     
23     [self setup];
24     
25     [self readShops];
26 }
27 
28 - (void)setup
29 {
30     // 初始化
31     NSString *path = @"/Users/apple/Desktop/shops.data";
32     self.db = [FMDatabase databaseWithPath:path];
33     [self.db open];
34     
35     // 2.创表
36     [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_shop (id integer PRIMARY KEY, shop blob NOT NULL);"];
37 }
38 
39 - (void)readShops
40 {
41     FMResultSet *set = [self.db executeQuery:@"SELECT * FROM t_shop LIMIT 10,10;"];
42     while (set.next) {
43         NSData *data = [set objectForColumnName:@"shop"];
44         HMShop *shop = [NSKeyedUnarchiver unarchiveObjectWithData:data];
45         NSLog(@"%@", shop);
46     }
47 //    NSMutableArray *shops = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/apple/Desktop/shops.data"];
48 //    NSLog(@"%@", [shops subarrayWithRange:NSMakeRange(20, 10)]);
49 }
50 
51 - (void)addShops
52 {
53 //    NSMutableArray *shops = [NSMutableArray array];
54 //    for (int i = 0; i<1000; i++) {
55 //        HMShop *shop = [[HMShop alloc] init];
56 //        shop.name = [NSString stringWithFormat:@"商品--%d", i];
57 //        shop.price = arc4random() % 10000;
58 //        [shops addObject:shop];
59 //    }
60 //    [NSKeyedArchiver archiveRootObject:shops toFile:@"/Users/apple/Desktop/shops.data"];
61     for (int i = 0; i<100; i++) {
62         HMShop *shop = [[HMShop alloc] init];
63         shop.name = [NSString stringWithFormat:@"商品--%d", i];
64         shop.price = arc4random() % 10000;
65         
66         NSData *data = [NSKeyedArchiver archivedDataWithRootObject:shop];
67         [self.db executeUpdateWithFormat:@"INSERT INTO t_shop(shop) VALUES (%@);", data];
68     }
69 }
70 
71 @end
原文地址:https://www.cnblogs.com/oc-bowen/p/5341704.html