2012-06-12 26 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语句中给出的列的索引。