標籤:firefox github 架構 資料庫 sqlite
db的資料包用從github上下載的三方架構進行解析和資料提取,格式一般為數組和字典。db的查看工具是firefox上的解析db外掛程式SQLite
三方架構為FMDB
#import "ViewController.h"//1. 引入標頭檔,需要引入libsqlite3的庫#import "FMDB.h"#import "Word.h"@interface ViewController () @end@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //2. 建立一個FMDatabase對象,並指定檔案路徑 FMDatabase *database = [FMDatabase databaseWithPath:@"/Users/apple/Desktop/japanese.db"]; //3. 開啟檔案 if (![database open]) { NSLog(@"Can not open file"); return; } //4. 執行查詢 FMResultSet *result = [database executeQuery:@"SELECT * FROM jp"]; NSMutableArray *array = [NSMutableArray array]; //5. 擷取表格中的一行 while ([result next]) {// NSMutableDictionary *dict = [NSMutableDictionary dictionary]; Word *word = [[Word alloc] init]; //根據名字(列名)擷取資料 int rid = [result intForColumn:@"id"]; NSLog(@"rid: %d", rid); word.rid = rid; // [dict setObject:[NSNumber numberWithInt:rid] forKey:@"id"]; //根據位置擷取資料 int rid1 = [result intForColumnIndex:0]; NSLog(@"rid1: %d", rid1); NSString *kana = [result stringForColumn:@"kana"]; NSLog(@"kana: %@", kana); // [dict setObject:kana forKey:@"kana"]; NSString *kana1 = [result stringForColumnIndex:5]; NSLog(@"kana1: %@", kana1); word.kana = kana1; [array addObject:word]; } //6. 關閉檔案 [database close]; NSLog(@"-----------"); NSLog(@"%@", array);}
這是word.h和word.m中的代碼
#import <Foundation/Foundation.h>@interface Word : NSObject@property (nonatomic, assign) NSInteger rid;@property (nonatomic, copy) NSString *kana;@end
#import "Word.h"@implementation Word- (NSString *)description{ return [NSString stringWithFormat:@"%@: rid: %d---kana: %@", [super description], _rid, _kana];}@end