首页 > 代码库 > db数据库利用第三方框架进行提取和解析数据

db数据库利用第三方框架进行提取和解析数据

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