Sql Query将NULL作为结果(期待文本)

时间:2012-06-12 10:24:10

标签: iphone ios database sqlite

-(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”

我的表名是“库存”

在上面的示例中,我有来自年份的硬编码值。

我在sqlite管理器中尝试了相同的sql查询,我发现数据显示所有的make名称。 我还发现sqlite3_column_type(compileStatement,5)返回5(SQLITE_NULL 5)但是根据我的Column类型它应该是3(SQLITE_TEXT 3)

有人可以提供上述行为的见解吗?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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  ;

}

答案 2 :(得分:0)

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