-2

我想从数组(imagearray)中的数据库中选择数据游标正在通过字典,但数据不存储在数组中......

-(void)getdataFromDatabase
{
    NSString *sqlQueryString =[NSString stringWithFormat:@"SELECT imageName,imageDetail,Date_Time FROM Image_Table"];
    const char *sql=[sqlQueryString UTF8String];

    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(delegate.DVCRS, sql, -1, &statement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSMutableDictionary *dicRD = [[NSMutableDictionary alloc]init];
            [dicRD setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)] forKey:@"imageName"];
            [dicRD setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)] forKey:@"imageDetail"];
            [dicRD setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)] forKey:@"Date_Time"];
            [imagearray addObject:dicRD];
            [imagenamearray addObject:dicRD];
        }
        NSLog(@"Main Object data  is %d",[imagearray count]);
    }
    else
    {
        NSLog(@"ERROR getdata %s",(char *)sqlite3_errmsg(delegate.DVCRS));
    }
    sqlite3_reset(statement);
    sqlite3_finalize(statement);
}
4

3 回答 3

0

如果在里面改变你的代码

     while (sqlite3_step(statement) == SQLITE_ROW)
            {
                NSMutableDictionary *dicRD = [[NSMutableDictionary alloc]init];
                [dicRD setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)] forKey:@"imageName"];
                [dicRD setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)] forKey:@"imageDetail"];
                [dicRD setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)] forKey:@"Date_Time"];
NSArarry *imagearray=[NSArray alloc]init;
                [imagearray addObject:dicRD];
                [imagenamearray addObject:dicRD];
            }
            NSLog(@"Main Object data  is %d",[imagearray count]);
于 2013-10-18T08:16:50.030 回答
0

我的第一个建议是您应该始终区分局部变量和属性。通过使用和下划线作为前缀(现在默认完成)或使用属性代替(self.propertyName)。如果可以,您应该更喜欢这种self.方法。

对于您的示例代码,我们不知道 imagearray 和 imagenamearray 的初始化位置。

第二个建议将第一个单词的字母大写:imageArray 和 imageNameArray。它更容易阅读。并保持一致性(imageName vs Date_Time)或使用常量。

您现在可以对字典和数组使用 Objective-C 2.0 语法的第三个也是最后一个建议。

所以 :

if (sqlite3_prepare_v2(delegate.DVCRS, sql, -1, &statement, NULL) == SQLITE_OK)
    {
        NSMutableArray *imageArray = [[NSMutableArray alloc] init];
        NSMutableArray *imageNameArray = [[NSMutableArray alloc] init];

        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSMutableDictionary *dicRD = [[NSMutableDictionary alloc]init];
            dicRD[@"imageName"] = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
            dicRD[@"imageDetail"] = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
            dicRD[@"dateTime"] = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
            [imagearray addObject:dicRD];
            [imagenamearray addObject:dicRD];
        }
        NSLog(@"Main Object data  is %d",[imagearray count]);
    }

甚至更好:

        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSDictionary *dicRD = @{
            "imageName"   : [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)],
            "imageDetail" : [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)],
            "dateTime"    : [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)]};
            [imagearray addObject:dicRD];
            [imagenamearray addObject:dicRD];
        }
于 2013-10-18T08:16:09.727 回答
-1

如果之前未初始化,请初始化您的数组 'imagearray' 和 'imagenamearray'。默认情况下它将设置为 nil。在开始 while 循环之前初始化。

于 2013-10-18T08:23:32.397 回答