2010-08-30 33 views
0

我有一個表有數據。如何從表中檢索多個記錄?

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

這一說法retrive我只有最後一條記錄。我如何獲得所有記錄?

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

使用包裝。它非常容易:http://github.com/ccgus/fmdb或http://github.com/schwa/TouchSQL – 2010-08-30 15:43:09

回答

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

正如彼得Hosey在評論中指出,這會給你一個字符串所有三個記錄都由空格分隔。您可能想要用這些記錄填充數組。

+0

@ennuikiller,yah,但是如何在變量中捕獲這些記錄。我正在使用aRecord,但它只存儲最後一條記錄。所以這是主要關心的問題。 – user12345 2010-08-30 15:06:18

+0

請參閱編輯答案 – ennuikiller 2010-08-30 15:21:29

+1

1.該代碼可能會崩潰,因爲您在循環之前從未將對象的指針指派給'myRecords',因此您將'stringByAppendingString:'發送給一個隨機指針。 2.即使你修正了這個問題,使用'stringByAppendingString:'將簡單地連接所有的值,比如「maxsamromjak」。你應該在這裏使用一個數組,並將單獨的字符串對象放入其中。 – 2010-08-30 18:33:41