1

我的应用在 .sqlite 文件中有 3,000 个考试题。并且这些问题是根据用户的需求动态选择和排序的(例如,“按错误答案计数排序”、“仅选择未访问的问题”)。

每当用户进行选择时,应用程序都会执行相应的 SQL 语句,并通过使用 sqlite3,将所有结果集发送到 NSMutableArray(问题类)。但正如您将注意到的,这是一个耗时的过程(大约 2~3 秒,并且 UI 在此过程中停止响应)。

所以我想创建一个具有 rowCount() 和 moveToRow(int index) 方法的“光标”类。

有了这个,我的想法是

Cursor c = [[Cursor alloc] init] query(
    "SELECT id,qtext,answer,a1,a2,... FROM TABLE WHERE id > 100"
)]; 
    // at this time, just a cursor is given, no need to iterate all the retrieved rows

for (i=0; i > c.rowCount(); i++) {
    c.moveToRow(i);
    ShowQuestionDetail(c);
}

像这样。

我知道 CoreData 适合这个目的,但我需要与这个应用程序的 android 版本共享 .sqlite 文件。CoreData 要求所有表名和字段名都以 Z_ 前缀开头,但我无法修改 .sqlite 文件的方案。我还需要使用 sqlcipher,而 CoreData 不能与 sqlcipher 一起使用。

FMDB 不支持提供检索行计数并移动到特定行的方法。

是否有任何其他支持此功能的 SQLite 包装库?

有人建议制作一个“目录”数组,其中只包含检索到的行的 id,并在每次调用 moveToRow() 时获取行。我同意这是一个很好的选择,但我想找到另一种方法。

4

1 回答 1

1

另一种方法可能是将您的查询限制为 1 行数据。那就是在 cellForRowAtIndex 进行调用。根据我的经验——最好有一个单独的方法——如果你真的需要,你甚至可以更进一步,添加一个 DataCache 层,用于根据 coredata 缓存到内存。

像这样的东西.... http://code.google.com/p/airband/source/browse/trunk/airband/Classes/DataCache.h?spec=svn103&r=103

例如。- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //初始化你的单元格

 NSDictionary *dict = [self questionDataForCellAtIndexPath:indexPath];
 // set the cell values
}



-(NSDictionary*)questionDataForCellAtIndexPath:(NSIndexPath *)indexPath{

        //ENTER_METHOD;
    NSDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:0];

    NSString  *qry = [NSString stringWithFormat: @"SELECT id,qtext,answer,a1,a2,... FROM TABLE WHERE id ORDER BY id DESC limit 1 offset %d", [indexPath row]];

// you could look up here for previously accessed rows from DataCache
    //  NSDictionary *cachedRow = [DATAENV.cache objectForKey:num];
///if (cachedRow == nil) {
    //  go get it
   //   else return it 

    DLog(@"qry%@",qry );
     EGODatabaseResult *result  = [appDelegate.userdb executeQuery:qry];

    if ([result count]==0) {
        return dict;
    }
    for(EGODatabaseRow *row in result) {
        [dict setValue:[row stringForColumn:@"name"]  forKey:@"name"];

    }
    return dict;
}

查看我的 fork 以获取 EgoDatabase。 https://github.com/jdp-global/egodatabase

它还包括非阻塞的异步方法。

于 2012-03-13T08:21:53.170 回答