2012-06-12 101 views
0
-(NSMutableArray*) fetchMakeNamesYear1:(int) year1 Year2:(int) year2 compileStaement: (sqlite3_stmt*)compiledStatement 
{ 
    NSMutableArray *makeNames=[NSMutableArray array]; 
    NSString *selectQuery=[NSString stringWithFormat:@"SELECT DISTINCT Make FROM Inventory WHERE [Year] BETWEEN 2009 AND 2012 AND Make IS NOT NULL ORDER BY Make",year1 ,year2 ]; 
// if (sqlite3_exec(((StorageManager*)[StorageManager sharedStorageManager]).database, [selectQuery cStringUsingEncoding:NSUTF8StringEncoding], NULL, NULL, NULL) == SQLITE_OK) 
// { 
//  
// } 
    if(sqlite3_prepare_v2(((StorageManager*)[StorageManager sharedStorageManager]).database, [selectQuery UTF8String] , -1, &compiledStatement, NULL) != SQLITE_OK) 
    { 
     return nil; 

    } 
    while (sqlite3_step(compiledStatement) == SQLITE_ROW) 
    { 

     NSLog(@"return TYpe: %d",sqlite3_column_type(compiledStatement, 5)); 

     if (sqlite3_column_text(compiledStatement, 5)!= NULL) 
     { 
      **NSString *makeName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];** 
      [makeNames addObject:makeName]; 

     } 
     sqlite3_reset(compiledStatement); 
} 
[self finalize:compiledStatement]; 

return makeNames ; 

}SQL查詢給予NULL作爲結果(期待文本)

由sqlite3_column_text返回的值(compiledStatement,5)是NULL。

在我的數據庫中,第5列的數據類型是nvarchar [50]。

它的列名是「製作」

我的表名是「庫存」

在上面的例子中我已經爲,並從今年硬編碼值。

我在sqlite管理器中嘗試了相同的sql查詢,我發現數據顯示所有make名稱。 我還發現sqlite3_column_type(compileStatement,5)回退5(SQLITE_NULL 5),但根據我的列類型它應該是3(SQLITE_TEXT 3)

有人可以洞察上述行爲嗎?

回答

1

您正在使用sqlite3_column_text錯列索引() - 列5不會在您的查詢,請試試0代替(見SQLite documentation

+0

謝謝,我在這裏犯了一個大錯。我是根據不同領域排列在表中的索引來理解專欄。保存了我的時間:) – Rahul

1
-(NSMutableArray*) fetchMakeNamesYear1:(int) year1 Year2:(int) year2 compileStaement: (sqlite3_stmt*)compiledStatement 
{ 
NSMutableArray *makeNames=[NSMutableArray array]; 
NSString *selectQuery=[NSString stringWithFormat:@"SELECT DISTINCT Make FROM Inventory WHERE [Year] BETWEEN 2009 AND 2012 AND Make IS NOT NULL ORDER BY Make",year1 ,year2 ]; 
// if (sqlite3_exec(((StorageManager*)[StorageManager sharedStorageManager]).database, [selectQuery cStringUsingEncoding:NSUTF8StringEncoding], NULL, NULL, NULL) == SQLITE_OK) 
// { 
//  
// } 
if(sqlite3_prepare_v2(((StorageManager*)[StorageManager sharedStorageManager]).database, [selectQuery UTF8String] , -1, &compiledStatement, NULL) != SQLITE_OK) 
{ 
    return nil; 

} 
while (sqlite3_step(compiledStatement) == SQLITE_ROW) 
{ 

    NSLog(@"return TYpe: %d",sqlite3_column_type(compiledStatement, 0)); 

    if (sqlite3_column_text(compiledStatement, 0)!= NULL) 
    { 
     **NSString *makeName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];** 
     [makeNames addObject:makeName]; 

    } 
    sqlite3_reset(compiledStatement); 
} 
[self finalize:compiledStatement]; 

return makeNames ; 

} 
0

在SELECT語句您選擇只有一個列即Make,因此sqlite3_column_text(compiledStatement, 0)中的列索引應該爲0而不是5.它不是數據庫中列(Make)的索引,而是您在select語句中給出的列的索引。