本文是主要实现了三个函数:
- testSQLite3 是测试系统自带的sqlite3的demo
- testFMDB是测试FMDB存取简单的数据类型的 的demo
- testFMDB2是将任意对象作为一个整体存入到FMDB的Demo
首先先定义了一个Person类,实现了<NSCoding>协议,对Person对象进行字段存取和整体存取
1 //Person.h 2 #import <Foundation/Foundation.h> 3 4 @interface Person : NSObject<NSCoding> 5 @property (nonatomic ,assign)int id; 6 @property (nonatomic ,copy) NSString *name; 7 @property (nonatomic ,assign)int age; 8 @end 9 10 //Person.m 11 #import "Person.h" 12 13 @implementation Person 14 -(NSString *)description{ 15 return [NSString stringWithFormat:@"id:%d--name:%@--age:%d",self.id,self.name,self.age]; 16 } 17 -(instancetype)initWithCoder:(NSCoder *)decoder{ 18 if (self = [super init]) { 19 self.id = [decoder decodeIntForKey:@"id"]; 20 self.name = [decoder decodeObjectForKey:@"name"]; 21 self.age = [decoder decodeIntForKey:@"age"]; 22 } 23 return self; 24 } 25 -(void)encodeWithCoder:(NSCoder *)encoder{ 26 [encoder encodeInt:self.id 27 forKey:@"id"]; 28 [encoder encodeObject:self.name forKey:@"name"]; 29 [encoder encodeInt:self.age forKey:@"age"]; 30 } 31 @end