如何从表中检索多个记录?

时间:2010-08-30 14:57:29

标签: objective-c cocoa

我有一张包含数据的表格。

id type
1  max
1  sam
1  rom
2  jak

我想检索type

所有的id=1;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                NSString *aRecord = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                NSLog (@"%@",aRecord);
            }

这句话只追溯了我的最后一条记录。我如何获得所有记录?

how to catch these records in the variable. I am using aRecord but it store only last record. So this is the main concern

1 个答案:

答案 0 :(得分:0)

sql ro这样做很简单:

select * from <table_name> where id = 1 

做这样的事情:

NSMutableString *myRecords = [NSMutableString initWithString:@""];

if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {   
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                NSString *aRecord = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];        

                myRecords = [myRecords stringByAppendingString:[NSString stringWithFormat:@"%@ ",arecord]];                
                NSLog (@"%@",aRecord);   
                }   

正如Peter Hosey在评论中指出的那样,这将给你一个字符串,其中所有三个记录用空格分隔。您可能希望用这些记录填充数组。