我需要对所有列进行搜索,因此我不想在where子句中指定每个列名,而是创建一个可以搜索所有列的方法。
-(void) fillSomeobjectNames:(NSString *)filter
{
FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
[db open];
NSString *sql = [NSString stringWithFormat:@"SELECT id, name, imagefile FROM %@ %@", mainTableName, filter];
FMResultSet *results = [db executeQuery:sql];
while([results next])
{
Someobject *someobject = [[Someobject alloc] init];
[someobject setID:[results intForColumn:@"id"]];
[someobject setName:[results stringForColumn:@"name"]];
NSString *iconName = [[results stringForColumn:@"imagefile"] stringByReplacingOccurrencesOfString:@".jpg" withString:@""];
[someobject setIconPath:[NSString stringWithFormat:@"%@-ico.jpg",iconName]];
[someobjects someobject];
}
[db close];
}
'filter'可以是'WHERE remark = [searchText] AND description = [searchText]依此类推......'
答案 0 :(得分:8)
我认为这不能用一个SELECT语句完成。获取列名称的最佳方法是PRAGMA table_info(table_name)
,遗憾的是,这不能在SELECT
语句中使用,因此您需要一个额外的SELECT
来获取列名,然后您可以构建sql并做你想做的事:
-(void) fillSomeobjectNames:(NSString *)filter
{
FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
[db open];
FMResultSet columnNamesSet* = [db executeQuery:[NSString stringWithFormat:@"PRAGMA table_info(%@)", mainTableName]];
NSMutableArray* columnNames = [[NSMutableArray alloc] init];
while ([columnNamesSet next]) {
[columnNames addObject:[columnNamesSet stringForColumn:@"name"]];
}
// put whatever you need to look for as mySearchPhrase
NSString* partToAppend = [NSString stringWithFormat:@"=%@ OR ", @"mySearchPhrase"];
NSString* filter = [columnNames componentsJoinedByString:partToAppend];
filter = [filter stringByAppendingString:[NSString stringWithFormat:@"=%@", @"mySearchPhrase"]];
// now your sql would look like this
NSString *sql = [NSString stringWithFormat:@"SELECT id, name, imagefile FROM %@ WHERE %@", mainTableName, filter];
FMResultSet *results = [db executeQuery:sql];
while([results next])
{
Someobject *someobject = [[Someobject alloc] init];
[someobject setID:[results intForColumn:@"id"]];
[someobject setName:[results stringForColumn:@"name"]];
NSString *iconName = [[results stringForColumn:@"imagefile"] stringByReplacingOccurrencesOfString:@".jpg" withString:@""];
[someobject setIconPath:[NSString stringWithFormat:@"%@-ico.jpg",iconName]];
[someobjects someobject];
}
[db close];
}
请注意,我已将WHERE
字移出过滤器,我还使用OR
代替AND
,这是我认为您打算这样做的。
要记住的另一件事是每列中的数据类型。您将每个字符串与字符串进行比较,但是您的表格中的每一列都是文本类型吗?我使用的pragma语句也返回给定列的类型(在名为 type 的列中),因此您可以使用它来过滤 text 类型的列
答案 1 :(得分:0)
感谢上面的回答,下面是swift 3的代码。这段代码显示了表的所有列。
let db = FMDatabase(path: Util.getPath(databasePath))
guard db != nil else { return }
db!.open()
let resultSet: FMResultSet! = db!.executeQuery("PRAGMA table_info(tableName)",withArgumentsIn: nil)
let marrInfo : NSMutableArray = NSMutableArray()
if (resultSet != nil) {
while resultSet.next() {
let aDict : NSMutableDictionary = NSMutableDictionary()
aDict.setValue(resultSet.string(forColumn: "cid"), forKey: "cid")
aDict.setValue(resultSet.string(forColumn: "name"), forKey: "name")
aDict.setValue(resultSet.string(forColumn: "type"), forKey: "type")
aDict.setValue(resultSet.string(forColumn: "notnull"), forKey: "notnull")
aDict.setValue(resultSet.string(forColumn: "pk"), forKey: "pk")
print(aDict)
marrInfo.add(aDict)
}
}
db!.close()