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  ;

}

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

在我的数据库中,第 5 列的数据类型是 nvarchar[50]。

它的列名是“Make”

我的表名是“库存”

在上面的示例中,我为 to 和 from 年份硬编码了值。

我在 sqlite manager 中尝试了相同的 sql 查询,我发现所有品牌名称的数据都显示。我还发现 sqlite3_column_type(compileStatement,5) retuns 5 (SQLITE_NULL 5) 但根据我的列类型它应该是 3 (SQLITE_TEXT 3)

有人可以对上述行为有所了解吗?

4

3 回答 3

1

您在 sqlite3_column_text() 中使用了错误的列索引 - 您的查询中不存在第 5 列,请尝试使用 0(请参阅SQLite 文档

于 2012-06-12T10:27:59.740 回答
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  ;

}
于 2012-06-12T10:47:20.993 回答
0

在您的选择语句中,您只选择一列是 Make,因此列索引sqlite3_column_text(compiledStatement, 0)应该是 0 而不是 5。它不是数据库中列(Make)的索引,而是您在 select 语句中给出的列的索引。

于 2012-06-12T10:57:58.047 回答