【发布时间】: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