从sqlite数据库中删除表

时间:2014-03-10 06:20:34

标签: ios objective-c fmdb

我无法使用DROP TABLE查询从sqlite数据库中删除表。已尝试过所有可能的解决方案,例如[database openCloseResultSet][resultSet close]等。但添加这些行导致内存不足错误,如果我不写这些行,那么我的程序只是停在drop table语句。也没有给出警告或错误。即使我把代码放在调试中,一旦执行此行,我的调试点就会消失。我正在使用FMDatabase库同时处理与sqlite相关的进程。我无法找到这个问题的原因。

也尝试了这些链接,但在我的情况下它们不起作用 1)FMDB executeUpdate DROP command does halt the app 2)How to remove all data from table using FMDB

这是我的代码,问题占主导地位。

+ (NSString *) deleteTable:(NSInteger) index{

NSString *returnFlag = @"success";
FMDatabase *database = nil;
@try {

    NSString *query = @"select name from sqlite_master where type = 'table'";

    database = [FMDatabase databaseWithPath:[DBOperator getDataBasePath:DATABASENAME]];

  if(database != nil && [database open])
  {
    FMResultSet *resultSet = [database executeQuery:query];

    while ([resultSet next]) {

        NSString *temp = [resultSet stringForColumnIndex: 0];

        if (index == DELETE_TYPE_ONE &&
            ([temp equalsIgnoreCase: TBUPW] ||
             [temp equalsIgnoreCase: TBCVR] ||
             [temp equalsIgnoreCase: TBCNTRLNO])) {

                [database executeUpdate:[NSString stringWithFormat:@"DROP TABLE %@", temp]];

        }else if(index == DELETE_TYPE_TWO && [temp hasPrefix:@"txn"]){

          [database executeUpdate:[NSString stringWithFormat:@"DROP TABLE %@", temp]];

        }else if(index == DELETE_TYPE_THREE &&
                 ([temp hasPrefix:@"t"] ||
                  [temp hasPrefix:@"T"] ||
                 [temp hasPrefix:@"ob"] ||
                 [temp hasPrefix:@"fb"] ||
                 [temp hasPrefix:@"cp"])){

                 NSlog("This is printed in console");

   [database executeUpdate:[NSString stringWithFormat:@"DROP TABLE %@", temp]];

     NSLog("This is not printed in console");

       }else if(index == DELETE_TYPE_FOUR && [temp equalsIgnoreCase:@"TBPLAN"]){

           [database executeUpdate:[NSString stringWithFormat:@"DROP TABLE %@", temp]];
        }
    }
  }
}
@catch (NSException *exception) {
    returnFlag = @"error";
}
@finally {
    [database close];
}

return returnFlag;
}

2 个答案:

答案 0 :(得分:0)

您可以使用此查询。

DROP TABLE YOUR_TABLE_NAME;

或者您可以使用此

DELETE FROM YOUR_TABLE_NAME;

答案 1 :(得分:0)

至于我跟随代码的工作原理,也许它会给你一个提示:

@property (nonatomic, strong, readonly) FMDatabaseQueue *database;

__block BOOL finishedSuccessfully = YES;
[database inTransaction:^(FMDatabase *db, BOOL *rollback) {
    // firstly let's remove the table:
    [db closeOpenResultSets];
    FMResultSet *deleteResultSet = [db executeQuery:@"DROP TABLE myTableName"];
    if ([deleteResultSet next]) {}
    [deleteResultSet close];
    finishedSuccessfully = !db.lastErrorCode && finishedSuccessfully; // breakpoint after this line

    // then let's perform anything on the db:
    FMResultSet *select = [db executeQuery:@"SELECT row FROM myTableName WHERE anotherRow = ?", @{2}];
    finishedSuccessfully = !db.lastErrorCode && finishedSuccessfully; // breakpoint after this line
}

我在第一部分之后没有错误,但第二部分返回的内容类似于"没有myTableName和#34;这样的表。