IOS小技巧——使用FMDB時如何把一個對像中的NSArray數組屬性存到表中

來源:互聯網
上載者:User

標籤: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數組屬性存到表中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.