fmdb realizes the function of adding, deleting, modifying and querying sqlite database (below)

The previous article introduced the use of FMDatabase in the fmdb framework to directly operate the sqlite database to realize the function of adding, deleting, modifying and querying the database. However, as mentioned earlier, if you use this type of multi-threaded access to the database, it will cause a project crash, so the FMDatabaseQueue class highlighted in this article is introduced. This class is thread-safe and can realize multi-threaded access.

A brief introduction to the use of FMDatabaseQueue

1. Create a global FMDatabaseQueue instance and instantiate it

// 懒加载数据库队列
- (FMDatabaseQueue *)queue {
    if (_queue == nil) {
        NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"userque.sqlite"];
        _queue = [FMDatabaseQueue databaseQueueWithPath:filePath];
    }
    return _queue;
}

2. Create the corresponding table userInfo, the fields in the table are still the primary key id, userid, name, age, sex, height, weight 7 fields. Using FMDatabaseQueue to operate database developers does not need to manually open and close the database.

// 创建表
- (BOOL)createTable{
    __block BOOL result;
    // 创建表
    [self.queue inDatabase:^(FMDatabase *db) {
        result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS userInfo(id integer PRIMARY KEY, userid text NOT NULL, name text NOT NULL, age integer, sex integer, height integer, weight integer);"];
    }];
    return result;
}

3. Insert data, pass in the dictionary, and the dictionary stores the information of a user

// 插入数据
- (BOOL)inserUserInfoWithDict:(NSDictionary *)userDict{
    __block BOOL result;
    [self.queue inDatabase:^(FMDatabase *db) {
        result = [db executeUpdateWithFormat:@"INSERT INTO userInfo(userid,name,age,sex,height,weight) VALUES(%@,%@,%ld,%ld,%ld,%ld)",userDict[@"userid"], userDict[@"name"], [userDict[@"age"] integerValue], [userDict[@"sex"] integerValue], [userDict[@"height"] integerValue], [userDict[@"weight"] integerValue]];
    }];
    return result;
}

4. Query the corresponding user information according to the corresponding userid

// 查询用户信息
- (NSMutableArray *)queryUserInfoWithUserid:(NSString *)userid{
    NSMutableArray *userArr = [NSMutableArray array];
    [self.queue inDatabase:^(FMDatabase *db) {
        FMResultSet *resultSet = [db executeQueryWithFormat:@"SELECT * FROM userInfo WHERE userid=%@", userid];
        while ([resultSet next]) {
            NSString *name = [resultSet stringForColumn:@"name"] ? : @"";
            NSInteger age = [resultSet intForColumn:@"age"];
            NSInteger sex = [resultSet intForColumn:@"sex"];
            NSInteger height = [resultSet intForColumn:@"height"];
            NSInteger weight = [resultSet intForColumn:@"weight"];
            NSDictionary *userDict = @{@"userid":userid, @"name":name, @"age":@(age), @"sex":@(sex), @"height":@(height), @"weight":@(weight)};
            [userArr addObject:userDict];
        }
    }];
    return userArr;
}

5. Modify the personal information of the corresponding userid user

// 修改用户信息
- (BOOL)updateUserInfo:(NSDictionary *)userDict userid:(NSString *)userid{
    __block BOOL result;
    [self.queue inDatabase:^(FMDatabase *db) {
        NSString *name = userDict[@"name"] ? : @"";
        NSInteger age = [userDict[@"age"] integerValue];
        NSInteger sex = [userDict[@"sex"] integerValue];
        NSInteger height = [userDict[@"height"] integerValue];
        NSInteger weight = [userDict[@"weight"] integerValue];

        NSString *sq = [NSString stringWithFormat:@"UPDATE userInfo SET name='%@', age='%ld', sex='%ld', height='%ld', weight='%ld' WHERE userid='%@'", name, age, sex, height, weight, userid];
        result = [db executeUpdate:sq];
    }];
    return result;
}

6. Delete the user corresponding to the userid

// 删除用户信息
- (BOOL)deleteUserInfoWithId:(NSString *)userid{
    __block BOOL result;
    [self.queue inDatabase:^(FMDatabase *db) {
        NSString *sq = [NSString stringWithFormat:@"delete from userInfo where userid='%@'",userid];
        result = [db executeUpdate:sq];
    }];
    return result;
}

end

The above is the method of using FMDatabaseQueue to operate the sqlite database, which can realize the function of adding, deleting, modifying and querying the database. The above methods can be called in multiple threads or used across threads, so it solves the multi-threading mentioned in the previous article. A crash problem when accessing the database.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325981790&siteId=291194637