【问题标题】:Deleting Table from sqlite database从 sqlite 数据库中删除表
【发布时间】:2014-03-10 06:20:33
【问题描述】:

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

也尝试过这些链接,但在我的情况下它们不起作用。
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;
}

【问题讨论】:

  • 你想做什么?从数据库中删除所有表?
  • 是的,如果我的条件是 Delete_Type_three,那么我想从我的 sqlite 数据库中删除所有以前缀“T”开头的表。 @Rashad
  • 您是否设置了异常断点,是否在您的main 中定义了异常回溯调用?
  • 我认为您应该在开始执行 DROP 之前将结果复制出来。当您在引用它时从 sqlite_master 中删除表时,SQLite 可能不好玩。

标签: ios objective-c fmdb


【解决方案1】:

您可以使用此查询。

DROP TABLE YOUR_TABLE_NAME;

或者你可以使用这个

DELETE FROM YOUR_TABLE_NAME;

【讨论】:

  • 已使用 DROP TABLE MY_TABLE_NAME 查询。在 code.temp 中看到的是我的数据库的每个表的名称。 @Ramani Ashish。
【解决方案2】:

至于我下面的代码有效,也许它会给你一个提示:

@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 这样的表”。

【讨论】:

    猜你喜欢
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多