【发布时间】:2013-02-27 02:06:01
【问题描述】:
我有一个 sqlitedb,其中有一个名为症状表的表,其中包含 ID (Integer) 和 SymptomName(String) 作为列。我创建了一个 sqlite 查询,我在其中根据症状名称的第一个字母获取症状名称
例如:- ..如果第一个字母包含字母 'A' 获取那些单词,'B' 获取那些单词等等 ..
现在我想从 ID 反转过程我想根据第一个字母获取 symptonName ...如果症状名称从字母“D”开始,我想获取从字母“D”开始的项目列表换句话说,我想获取由 ID 的第一个字母组成的 SymptomName 列表 .. 并返回 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;
}
【问题讨论】:
-
为什么不以排序形式获取所有记录。然后按首字母分组?
-
试过
select * from Symptom where Symptom like '@%%'?我不知道 Obj-c 查询的语法,但是喜欢字符串%@%%开头的 % 允许在您的第一个字母之前使用其他字符。 -
araknoid ..thats my sqlite statment .
-
yunas ...当我输入一个症状名 ...我需要获取第一个字母
标签: ios objective-c sqlite