jeudi 1 janvier 2015

Can't read integer from local sqlite3 database

I'm new to sqlite on Xcode. Could you please help me with sqlite on Xcode problem? I've been trying to get integer data from sqlite database on Xcode but it always end up being 0. I know they are not 0 because I checked the columns with the terminal.


my sqlite question table looks like bellow. I can access to the texts but not integers.



CREATE TABLE questions (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, question TEXT NOT NULL, choice1 TEXT NOT NULL, choice2 TEXT NOT NULL, choice3 TEXT NOT NULL, choice4 TEXT NOT NULL, answer INTEGER NOT NULL, series INTEGER NOT NULL DEFAULT 0, difficulty INTEGER NOT NULL DEFAULT 0, complete INTEGER NOT NULL DEFAULT 0);


my source codes are bellow





  • (NSMutableArray *)selectSql:(NSString *)sql {



    NSDictionary *resultDic;
    resultDic = [NSDictionary new];
    NSMutableArray *resultArray;
    resultArray = [NSMutableArray new];


    NSString *databaseName = @"quiz.sqlite";


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,


    NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; NSString *databasePath = [path stringByAppendingPathComponent:databaseName];



    sqlite3 *database;
    sqlite3_stmt *statement;

    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

    int result = sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL);

    if (result != SQLITE_OK) {
    NSLog(@"Failed to SQLite compile.");
    return resultArray;
    }

    while (sqlite3_step(statement) == SQLITE_ROW) {

    NSString *str0 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
    NSString *str1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
    NSString *str2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
    NSString *str3 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
    NSString *str4 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 4)];

    resultDic = @{@"int0":([NSNumber numberWithInt:sqlite3_column_int(statement, 0)]),
    @"int1":([NSNumber numberWithInt:sqlite3_column_int(statement, 1)]),
    @"int2":([NSNumber numberWithInt:sqlite3_column_int(statement, 2)]),
    @"int3":([NSNumber numberWithInt:sqlite3_column_int(statement, 3)]),
    @"text0":str0,
    @"text1":str1,
    @"text2":str2,
    @"text3":str3,
    @"text4":str4,
    };
    if (sqlite3_column_type(statement, 0) == SQLITE_NULL)
    {NSLog(@"IT IS NULL");
    }else{
    NSLog(@"IT IS NOT NULL");
    }

    NSLog(@"%@", resultDic);
    [resultArray addObject:resultDic];
    }

    sqlite3_close(database);
    } else {

    NSLog(@"Can't open database.");
    }

    return resultArray ;

    }




The array is giving me (_NSCFNumber*)(int)0. I found out NSCF is something to do with JSON but I couldn't find out more.


I've been working on this for 2 days now. I hope you could help. Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire