开发者

How to retrieve multiple records from table?

I have a table having data.

id type
1  max
1  sam
1  rom
2  jak

I want to retrieve all the type where 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);
            }

this statement retrive me only the last record. How can i get all the records?

how to catch these records in the variable. I am using aRecord but it store only last record. So this is the ma开发者_C百科in concern


The sql ro do this is simply:

select * from <table_name> where id = 1 

do something like this:

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);   
                }   

As Peter Hosey points out in the comments, this will give you one string with all three records separated by a space. You probably want to populate an array with these records instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜