1

すべての列を検索する必要があるため、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] AND etc...'にすることができます。

4

2 に答える 2

8

これは、単一のSELECTステートメントでは実行できないと思います。列名を取得する最良の方法は、残念ながらこれをステートメントでPRAGMA table_info(table_name)使用できないため、列名を取得するためにさらに1つ必要です。その後、SQLを作成して、必要な処理を実行できます。SELECTSELECT

-(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ください。これは、あなたが意図したことだと思います。

覚えておくべきもう1つのことは、各列のデータ型です。それぞれを文字列と比較しますが、テーブルの各列はテキスト型ですか?私が使用したプラグマステートメントは、指定された列のタイプ( typeという名前の列)も返すため、これを使用して、テキスト型の列をフィルター処理できます。

于 2012-01-14T19:50:51.293 に答える
0

上記の回答をありがとう、swift3の以下のコード。このコードは表のすべての列を表示します。

    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()
于 2017-12-21T13:03:32.257 に答える