TOP

IOS开发-本地持久化存储sqlite应用(二)
2017-10-13 10:09:41 】 浏览:10120
Tags:IOS 开发 本地 持久化 存储 sqlite 应用

.h

//
//  ViewController.h
//  Lesson02
//
//  Created by ouyangjunqiu on 16/4/7.
//  Copyright © 2016年 oshine. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "fmdb/FMDB.h"
#import <sqlite3.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *label;

- (IBAction)createTable:(id)sender;

- (IBAction)initRole:(id)sender;

- (IBAction)readProfile:(id)sender;

@end

 

ViewController.m

 

//
//  ViewController.m
//  Lesson02
//
//  Created by ouyangjunqiu on 16/4/7.
//  Copyright © 2016年 oshine. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)createTable:(id)sender {
    FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
    
    if (![db open]) {
         NSLog(@"OPEN FAIL");
        return;
    }
    
    [db executeUpdate:@"CREATE TABLE IF NOT EXISTS profile(name text,val text)"];
    [db close];
}

- (IBAction)initRole:(id)sender {
    FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
    
    if (![db open]) {
        NSLog(@"OPEN FAIL");
        return;
    }
    
    [db beginTransaction];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"HP",@"600"];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"MP",@"250"];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"攻击",@"70"];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"防御",@"1"];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"攻速",@"0.3"];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"移速",@"320"];
    [db commit];
    [db close];
    
}

- (IBAction)readProfile:(id)sender{
    FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
    
    if (![db open]) {
        NSLog(@"OPEN FAIL");
        return;
    }
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    FMResultSet *rs = [db executeQuery:@"SELECT name,val FROM profile"];
    while ([rs next]) {
        [dictionary setObject:[rs stringForColumn:@"val"] forKey:[rs stringForColumn:@"name"]];
    }
    [rs close];
    [db close];
    
    [self show:dictionary];
}


-(void)show:(NSMutableDictionary *)dictionary {
    
    self.label.numberOfLines = 0;
    
    NSString * text = [[NSString alloc] init];
    for(NSString *key in dictionary) {
        text = [NSString stringWithFormat:@"%@%@:%@\n",text,key,[dictionary objectForKey:key]];
    }
    self.label.text = text;
}

@end

案例结束

 

NSMutableDictionary Class Reference (key->value可增长数组)

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundati
IOS开发-本地持久化存储sqlite应用(二) https://www.cppentry.com/bencandy.php?fid=99&id=159008

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇IOS开发-本地持久化存储sqlite应.. 下一篇iOS 开发 - 改善APP的流畅度 (..