2014-10-31 86 views
0

這是我的代碼。 它只返回數據庫的第一行。我知道有些事情是錯的,但不知道是什麼。請幫忙。如何將數據從sqlite讀取到數組中

if(sqlite3_open([databasePath UTF8String],&texttalkdb)==SQLITE_OK) 
{ 
    sqlite3_stmt *statement; 

    if(sqlite3_prepare_v2(texttalkdb, [sql UTF8String], -1,&statement, NULL)==SQLITE_OK) 
    { 
     if(sqlite3_step(statement)==SQLITE_ROW) 
     { 
      for(int i=0;i<=20;i++) 
     { 
      char *pass=(char*)sqlite3_column_text(statement,i); 
      NSString *passStr = [NSString stringWithFormat:@"%s",pass]; 

       NSString *msg=[[NSString alloc]initWithUTF8String:pass]; 
       [arr addObject:msg]; 
      } 
     } 
     sqlite3_finalize(statement); 
    } 
    sqlite3_close(texttalkdb); 
} 
NSLog(@"%@",arr); 
return arr; 

回答

2

嘗試此

if(sqlite3_prepare_v2(texttalkdb, [sql UTF8String], -1,&statement, NULL)==SQLITE_OK) 
{ 
      while(sqlite3_step(statement) == SQLITE_ROW) 
      { 
      //get records 
     } 
} 
+1

1爲適當的指示。 – Kampai 2014-10-31 09:55:54

0

步驟1:這是檢查獲取值是空的或不是

+(NSString*)charToString:(char*)chart 
{ 
    NSString *string = @" "; 
    if(string) 
    { 
     chart = [self checkEmptyChar:chart]; 
     string=[NSString stringWithUTF8String:chart]; 
    } 
    return string; 
} 

步驟2:這對於檢查字符

+(char *)checkEmptyChar:(char *)check 
{ 
    NSString *string = @" "; 
    if (check == NULL) 
    check = string; 
    return check; 
} 

步驟3:要提取的SQLite編碼

if (sqlite3_prepare_v2(database, [query UTF8String], -1, &stment, nil) == SQLITE_OK) 

    { 
     while (sqlite3_step(stment) == SQLITE_ROW) 

     { 

      for(int i=0;i<=20;i++) 

      { 
       NSString *msg = [self charToString: (char *)sqlite3_column_text(stment, i)]; 

       [array addObject:msg]; 
      } 

     } 

     sqlite3_reset(stment); 
    } 

    sqlite3_finalize(stment); 
} 

sqlite3_close(database); 
相關問題