FMDB的使用

2016-02-25  本文已影响147人  风灵子偌

简介

使用

  1. github地址地址进行下载源代码
  2. 注意点
  1. 书写代码。
//存储数据库的路径
    NSString *mainPath =   [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [mainPath stringByAppendingPathComponent:@"first.sql"];
    self.dataBase = [FMDatabase databaseWithPath:filePath];
    //对数据库进行操作
    if (![self.dataBase open]) {
        NSLog(@"数据库打开失败");
    }else{
        NSLog(@"数据库打开成功");
        BOOL result =  [self.dataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS  t_students ( idd integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL)"];
        if (result) {
            NSLog(@"创建表成功");
        }else{
            NSLog(@"创建表失败");
        }
    }
    // Do any additional setup after loading the view, typically from a nib.
}

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


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self insertData];
    [self deleteData];
    [self query];
}

- (void)insertData{
    
    for (int i = 0; i < 10; i ++) {
        NSString *sting = [NSString stringWithFormat:@"zyan%d",i];
        [self.dataBase executeUpdate:@"INSERT INTO t_students (id,name,age) VALUES (%ld,%@,%d);", i, sting , arc4random_uniform(10)];
//        [self.dataBase executeUpdate:@"INSERT INTO t_students (id,name, age) VALUES (?, ?, ?);", @(i), sting,@(arc4random_uniform(40))];
    }
    
}

- (void)deleteData{
    [self.dataBase executeUpdate:@"DELETE * FROM t_students"];
}


- (void)query{
    FMResultSet *set = [self.dataBase executeQuery:@"SELECT * FROM t_students"];
    while ([set next]) {
        int number = [set intForColumn:@"id"];
        NSString *string = [set stringForColumn:@"name"];
        NSLog(@"%d,%@",number,string);
    }
}

@interface ViewController ()
/**
 *  数据库操作对象队列
 */
@property (strong, nonatomic) FMDatabaseQueue *dataQueue;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //存储数据库的路径
    NSString *mainPath =   [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [mainPath stringByAppendingPathComponent:@"first.sql"];
    //创建数据库操作对象队列
    self.dataQueue = [FMDatabaseQueue databaseQueueWithPath:filePath];
    [self.dataQueue inDatabase:^(FMDatabase *db) {
        BOOL result =  [db executeUpdate:@"CREATE TABLE IF NOT EXISTS  t_students ( idd integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL)"];
        if (result) {
            NSLog(@"创建表成功");
        }else{
            NSLog(@"创建表失败");
        }
        // Do any additional setup after loading the view, typically from a nib.
    }];
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self insertData];
//    [self deleteData];
    [self query];
}

- (void)insertData{
    
    for (int i = 0; i < 10; i ++) {
        NSString *sting = [NSString stringWithFormat:@"zyan%d",i];
        [self.dataQueue inDatabase:^(FMDatabase *db) {
//            [db executeUpdate:@"INSERT INTO t_students (id,name,age) VALUES (%ld,%@,%d);", i, sting , arc4random_uniform(10)];
        [db executeUpdate:@"INSERT INTO t_students (idd,name, age) VALUES (?, ?, ?);", @(i), sting,@(arc4random_uniform(40))];
        }];
    }
    
}

- (void)deleteData{
    [self.dataQueue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"DELETE * FROM t_students"];
    }];
}


- (void)query{
    [self.dataQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *set = [db executeQuery:@"SELECT * FROM t_students"];
        while ([set next]) {
            int number = [set intForColumn:@"idd"];
            NSString *string = [set stringForColumn:@"name"];
            NSLog(@"%d,%@",number,string);
        }
    }];
}
上一篇下一篇

猜你喜欢

热点阅读