標籤:tail tty tab art ios comm ras pat into
http://blog.csdn.net/github_29614995/article/details/46797917
在開發的當中,往往碰到要將資料持久化的時候用到FMDB,但是碰到模型中的屬性有數組,但是FMDB存放的屬性通常只為Text,那我們改怎麼辦呢?
思路:1.建立一個表,先將模型的除數組以為的其他屬性寫入表中,名為t_groupBuyModel
2.建立一個表裝載模型中的數組文本,名為t_images
3.當存入模型到表中的時候,遍曆模型中的數組屬性,讓每一條資料都存入t_images表中去
// 既然要使用到FMDB,肯定必須包含FMDB的標頭檔把,這些東西我就不多說了,直接奔重點#import "FMDatabase.h"> // 小編比較喜歡在對象的initialize方法裡面創表(ps:好奇怪,發部落格不寫">"代碼顏色不會變的,什麼問題呢)static FMDatabase *_db;+ (void)initialize{ NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"AJKGroupBuyModels.db"]; _db =[ FMDatabase databaseWithPath:path]; [_db open]; // 創表 [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_groupBuyModel (id integer PRIMARY KEY,act_title text,cover_image text ,avg_price text,region_name text,join_num text,date_end text,act_rebate text,loupan_id text,loupan_name text,address text,see_detail text);"]; // 建立一個表裝載模型中的數組文本 [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_images (id interger PRIMARY KEY ,loupan_id text, image text)"];}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
———- 華麗的分割線
+ (void)addModel:(AJKGroupBuyModel *)model{ [_db executeUpdateWithFormat:@"INSERT INTO t_groupBuyModel (act_title,cover_image,avg_price,region_name,join_num,date_end,act_rebate,loupan_id,loupan_name,address,see_detail) VALUES (%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@);",model.act_title,model.cover_image,model.avg_price,model.region_name,model.join_num,model.date_end,model.act_rebate,model.loupan_id,model.loupan_name,model.address,model.see_detail]; // 根據對應的模型遍曆數組內的對象並建立資料 for (int i = 0; i < model.images.count; i ++ ) { [_db executeUpdateWithFormat:@"INSERT INTO t_images (loupan_id,image) values (%@,%@)",model.loupan_id,model.images[i]]; }}
IOS小技巧——使用FMDB時如何把一個對像中的NSArray數組屬性存到表中