English 中文(简体)
如何通过在目标-c中使用圆顶的板q中的所有栏目加以消化
原标题:how to iterate through all the columns in sqlite using fmdb in objective-c
  • 时间:2012-01-12 06:04:34
  •  标签:
  • fmdb

我需要对所有栏目进行搜索,而不是在我想要制定一种能够通过所有栏目搜索的方法的栏目中注明每个栏目。

-(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];
}

过滤器可以是WHERE 标记=[研究范围]和描述=[研究范围]等。

最佳回答

我不认为可以通过一次选举考试发言来做到这一点。 获取栏目名称的最佳途径是PRAGMA table_info(table_name),但遗憾的是,这不能在的表中使用,因此,你需要增加一个<代码>SlectT,以获得栏目名称,然后可以建立你的舱面并做你想要做的事情:

-(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表也回归了该栏的类型(见 类型栏),因此,你可以用来过滤text的栏目。

问题回答

感谢上述答复,感谢迅速制定简便法 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()




相关问题
deleting row in SQLite 3

In UITableView, I want to delete a row and remove that row on database, but my query of sqlite don t works. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)...

FMDB query is not taking any effect on the DB

I am using FMDB in my app to do some updates in my DB. Problem is when I update my table with executeUpdate function, it update my table BUT if I copy the same .sqlite file from project to at another ...

FMDB open database

I m trying to open a database that I have in my project inside Resources. The problem is that it seems that its impossible to find the database file! I tried with the complete path, and it works, but ...

FMDB SQLite question: row count of a query?

does anyone know how to return the count of a query when using FMDB? If I executeQuery @"select count(*) from sometable were..." I get an empty FMResultSet back. How can I get the row count of the ...

Writing to sqlite db using fmdb?

Ok so I understand that fmdb is totally outdated, and core data is where it s at, but for now, I just need to tweak an app I m working on that uses fmdb to read from a sqlite db in the resources ...

How do I import a class into an iPhone project?

I m sure this is very easy but I m trying to get the fmdb sqlite wrapper to work in an iPhone project. I ve grabbed the files via SVN, and there are h and m files inside an src folder. I d ...

热门标签