FMDB puts objects into the database, and fmdb Object Database
First, create an HJShop class to implement the NSCoding protocol.
# Import <Foundation/Foundation. h>
@ Interface HJShop: NSObject <NSCoding>
@ Property (nonatomic, copy) NSString * name;
@ Property (nonatomic, assign) double price;
@ End
# Import "HJShop. h"
@ Implementation HJShop
-(Void) encodeWithCoder :( NSCoder *) ACO
{
[Ecoder encodeObject: self. name forKey: @ "name"];
[Ecoder encodeDouble: self. price forKey: @ "price"];
}
-(Id) initWithCoder :( NSCoder *) aDecoder
{
If (self = [super init]) {
Self. name = [aDecoder decodeObjectForKey: @ "name"];
Self. price = [aDecoder decodeDoubleForKey: @ "price"];
}
Return self;
}
-(NSString *) description
{
Return [NSString stringWithFormat: @ "% @-% f", self. name, self. price];
}
@ End
-----------------------------------------------------
Then use the FMDB framework
// Initialization
NSString * path = [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "shop. sqlite"];
Self. db = [FMDatabase databaseWithPath: path];
[Self. db open];
// Create a table
[Self. db executeUpdate: @ "create table if not exists t_shop (id integer primary key, shop blob not null unique);"];
[Self addShops];
[Self readShop];
-(Void) addShops
{
HJShop * shop = [[HJShop alloc] init];
Shop. name = @ "jack ";
Shop. price = 11.0;
NSData * data = [NSKeyedArchiver archivedDataWithRootObject: shop];
[Self. db executeUpdateWithFormat: @ "insert or ignore into t_shop (shop) VALUES (% @);", data];
}
-(Void) readShop
{
FMResultSet * set = [self. db executeQuery: @ "SELECT * FROM t_shop;"];
While (set. next ){
NSData * data = [set objectForColumnName: @ "shop"];
HJShop * shop = [NSKeyedUnarchiver unarchiveObjectWithData: data];
NSLog (@ "% @", shop );
}
}
As long as the object encoding is converted into a binary file, it can be put into SQLite.