FMDB puts objects into the database, and fmdb Object Database

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.