如何从sqlite db查询中的第一个字符获取字符串

时间:2013-02-26 16:52:28

标签: ios objective-c sqlite

我有一个sqlitedb,它有一个名为symptomtable的表,其中包含ID(整数)和SymptomName(String)作为列。我创建了一个sqlite查询,我根据symptomName的第一个字母提取SymptomName

对于ex: - ..如果第一个字母包含字母'A'取这些单词,'B'取这些单词等等。

现在我想从ID转换进程我希望根据第一个字母获取symptonName ...如果症状名称从字母“D”开始,我想获得从字母“D”开始的项目列表其他词我想获取SymptomName列表,其中包含来自ID的第一个字母..并返回Indexpath

在查询中NSString * alphabetString = [NSString stringWithFormat:@“%c”,i];从这段代码我得到了性格。

-(NSMutableDictionary *)indexReadData
{
    NSMutableDictionary *indexDict=[[NSMutableDictionary alloc]init];
    int i;  
    NSString *dbPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"QuickCureNew1.sqlite"];
    if (sqlite3_open([dbPath UTF8String], &quickAppNew)==SQLITE_OK) 
    {
        for (i=65; i<=90; i++) 
        {
            NSString *alphabetString=[NSString stringWithFormat:@"%c",i];
            NSString *sqlStmtReadIndexSymptom=[NSString stringWithFormat:@"select * from Symptom where Symptom like '%@%%'",alphabetString];
            const char *sqlCharReadIndexSymptom=[sqlStmtReadIndexSymptom UTF8String];
            sqlite3_stmt *selectStmtReadIndexSymptom;
            if (sqlite3_prepare_v2(quickAppNew, sqlCharReadIndexSymptom, -1, &selectStmtReadIndexSymptom, NULL)==SQLITE_OK)
            {
                NSMutableArray *indexArray=[[NSMutableArray alloc] init];
                while (sqlite3_step(selectStmtReadIndexSymptom)==SQLITE_ROW) 
                {
                    indexIdNo=[[NSNumber alloc]initWithInt:(int)sqlite3_column_int(selectStmtReadIndexSymptom,0)];
                    symptomIndexTxt=[[NSString alloc] initWithUTF8String:(char*) sqlite3_column_text(selectStmtReadIndexSymptom, 1)];
                    symptomsIndexDict=[[NSMutableDictionary alloc] initWithObjectsAndKeys: 
                                       symptomIndexTxt,@"SymptIndexName",
                                       indexIdNo,@"SymptIndexID",nil];
                    [indexArray addObject:symptomsIndexDict];
                }
                if (indexArray.count>0) 
                {
                    [indexDict setObject:indexArray forKey:alphabetString];
                }
            }
        }
        sqlite3_close(quickAppNew);
    }
    return indexDict;    
}

1 个答案:

答案 0 :(得分:0)

对于CoreData,它是NSPredicate

NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'a'"];

这一切都在Apple Developer参考中: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html

也许这会指出你正确的方向。