2015-09-16 25 views
1

我有一個數據庫有一個'單詞'表(存儲單詞)的視圖,我想檢索並將數據發送到另一個ViewController,而不是接收實際的詞,我得到idViewController接收ID而不是SQLite查詢中的數據

這裏是我的代碼:

(void)getListData { 

//tableData = [[NSMutableArray alloc]init]; 
sqlite3_stmt *statement; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dexternotedb.sqlite"]; 

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) { 
    WordListViewController *temp = APP_DELEGATE.wordListViewController; 
    NSLog(@"word id %ld",(long)[temp.wordId integerValue]); 

    NSString *sql=[NSString stringWithFormat:@"SELECT * FROM words WHERE word_id=\"%@\"",APP_DELEGATE.wordListViewController.wordId]; 
    const char *read_stmt = [sql UTF8String]; 
    NSLog(@"select query%@",sql); 


    if(sqlite3_prepare_v2(database, read_stmt, -1, &statement, NULL) == SQLITE_OK){ 
     sqlite3_bind_int(statement, 1, [temp.wordId integerValue]); 

     while(sqlite3_step(statement) == SQLITE_ROW) { 
      NSString *word1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 
      [word addObject:word1]; 
      NSString *meaning1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 
      [meaning addObject:meaning1]; 

      NSString *sentence1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 
      [Sentence addObject:sentence1]; 

     } 


    sqlite3_finalize(statement); 
} else { 
      sqlite3_close(database); 
      NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database)); 

} 

回答

0

第一:

不要使用「SELECT *」,因爲你真的不知道該列的順序,並且使用的是列索引來訪問它們。

所以,你應該使用真實姓名 「選擇,ID,字......」

二:

你的所有列檢索都指向同一個索引0:

[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 

這就是爲什麼你得到相同的數據(ID),因爲它只有第一個索引。所以將其更改爲正確的索引1,2,3,而且你會得到正確的DATAS ..

使用該SQL(檢查列名是正確的)

SELECT word_id, word, meaning, sentence FROM words WHERE word_id=\"%@\" 

然後你就可以使用正確的索引爲sqlite3_column_text:

NSString *word1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]; 
      [word addObject:word1]; 
      NSString *meaning1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)]; 
      [meaning addObject:meaning1]; 

      NSString *sentence1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)]; 
      [Sentence addObject:sentence1]; 
+0

我有詞義和句子中的wordID字表...所以u能告訴我如何,如果你說的列名正確地寫它 – Asheesh

+0

選擇查詢,在這裏:選擇wordid,單詞,含義,句子FROM words WHERE word_id = \「%@ \ –

+0

檢查更新,重要的是將正確的索引添加到sqlite3_column_text函數中。還要檢查查詢中的列名 –