对Sqlite数据库一些操作

一.根据表名删除数据表

- (BOOL)clearDB
{
    if (self.dataBase == nil || !self.dataBase.open)
    {
        DLog(@"--db打开错误--");
        return NO;
    }
    //1.查询所有表名称
    NSString *sql = [NSString stringWithFormat:@"SELECT name FROM sqlite_master WHERE type='table' order by name"];
    
    FMResultSet *rs  = [self.dataBase executeQuery:sql];
    NSMutableArray *tableNames = [NSMutableArray array];
    
    while (rs &&[rs next])
    {
        NSString *tableName =[rs stringForColumn:@"name"];
        
        [tableNames addObject:tableName];
    }
    //2.过滤不删除的表名
    NSString * saveTableName1 = [NSString stringWithFormat:@"LoginData"];
//    NSString * saveTableName2 = [NSString stringWithFormat:@"PushMessage"];
//    NSString * saveTableName3 = [NSString stringWithFormat:@"Notice"];
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF != %@ && SELF != %@ && SELF != %@",saveTableName1,saveTableName2,saveTableName3];
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF != %@ ",saveTableName1];
    NSArray *delNames = [tableNames  filteredArrayUsingPredicate:predicate];
   __block BOOL ret;
    [delNames enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj && [obj isKindOfClass:[NSString class]]) {
            NSString *delSqlStr = [NSString stringWithFormat:@"DROP TABLE IF EXISTS %@;",obj];
            DLog(@"-delSqlStr--%@",delSqlStr);
            ret =  [self.dataBase executeUpdate:delSqlStr];
            [rs close];
        }
    }];
    return ret;
}

二,数据库添加新字段

#pragma mark 存储新的字段 updatetime;
+(void)addNewColumn
{
    XKDataBase *dbHandler = [XKDataBase shared];
    if (![dbHandler openDB]) {
        return ;
    }

    if (![dbHandler.dataBase tableExists:@"Notice"]) {
        DLog(@"Notice表不存在");
        return ;
    }
    NSMutableArray *tmpArray = [NSMutableArray array];
    FMResultSet *rs = [dbHandler.dataBase executeQuery:@"pragma table_info([Notice]) "];
    while ([rs next]) {
        NSString *culmnName = [rs stringForColumnIndex:1];

        [tmpArray addObject:culmnName];
    }
    [rs close];
    if (![tmpArray containsObject:@"updatetime"]) {
        NSString *sql = [NSString stringWithFormat:@"ALTER TABLE Notice ADD COLUMN updatetime int"];
        BOOL ret =  [dbHandler.dataBase executeUpdate:sql];

        DLog(@"插入updatetime  ret = %d",ret);
    }

}

三.检查表中 是否含有某个字段

+(BOOL)checkTable:(NSString *)tableName comtainColumn:(NSString*)columnName
{
    XKDataBase *dbHandler = [XKDataBase shared];
    
    if (![dbHandler openDB]) {
        DLog(@"数据库未打开");
        return YES;
    }
    if (![dbHandler.dataBase tableExists:tableName]) {
        DLog(@"checkUpdatetimeColumn %@表不存在",tableName);
        return YES;
    }
    NSString *sql = [NSString stringWithFormat:@"pragma table_info([%@])",tableName];
    NSMutableArray *tmpArray = [NSMutableArray array];
    FMResultSet *rs = [dbHandler.dataBase executeQuery:sql];
    while ([rs next]) {
        NSString *culmnName = [rs stringForColumnIndex:1];
        
        [tmpArray addObject:culmnName];
    }
    [rs close];
    //包含字段
    if ([tmpArray containsObject:columnName]) {
        return YES;
    }else
    {
        return NO;
    }
}```

你可能感兴趣的:(对Sqlite数据库一些操作)